Core syntax · 10 sample answers

Python functions: practice questions with answers.

A function is not just a block of code with a name. It is a boundary: values go in, a result comes out, and local names should not leak. These questions test that boundary.

35 questions in the full bank · 13 choice · 13 fill · 9 output

What this topic tests

  • Distinguish printing a value from returning it.
  • Trace arguments, defaults, and local versus global names.
  • Use lambda for short callbacks without hiding the main logic.

Common mistakes

  • Using print inside a function when the caller needs a returned value.
  • Using a mutable default such as [] and being surprised when it persists.
  • Forgetting that a function without return gives back None.
Sample questions

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0644

    1. Ming writes a function that prints a menu but has no return statement. What does a call return?

    1. raises an error
    2. 0
    3. None
    4. an empty string
    Show answer

    Answer

    C. None

    Why: A function without a return statement still returns a value, and that value is None. This is different from printing: the call produces None while print has already sent text to the screen.

  2. Multiple choiceq0651

    2. 🧋 Ming writes a menu-printing function. What is the return value of `def f(): pass`?

    1. None
    2. 0
    3. raises an error
    4. an empty string
    Show answer

    Answer

    A. None

    Why: pass does nothing, and with no return statement the call still produces None. A function used only for its side effects always returns None.

  3. Multiple choiceq0655

    3. 🧋 What does this code print? count=0 def f(): global count count+=1 f() print(count)

    1. 0
    2. 1
    3. raises an error
    4. None
    Show answer

    Answer

    B. 1

    Why: The declaration lets the body rebind the module-level count, so count += 1 changes the outer value from 0 to 1. Without global this line would raise UnboundLocalError.

  4. Multiple choiceq0665

    4. What does a function with no return statement return?

    1. 0
    2. an empty string
    3. None
    4. False
    Show answer

    Answer

    C. None

    Why: None is the implicit result of every function that ends without return, or with a bare return. It is not 0 and not an empty string.

  5. Fill in the blankq0646

    5. A temporary variable defined inside a function, such as one drink's ingredients, is called a ___ variable.

    Show answer

    Answer

    local

    Why: A name assigned inside a function lives in that call's local namespace and disappears when the call returns. The rest of the file cannot see it.

  6. Fill in the blankq0659

    6. 🧋 A variable defined inside a function is called a ___ variable.

    Show answer

    Answer

    local

    Why: A local variable is only reachable inside the function that created it, which is why functions can reuse common names like total without clashing.

  7. Fill in the blankq0670

    7. Accept any number of positional arguments. What goes in the blank?

    def total(*____):
        return sum(prices)
    Show answer

    Answer

    prices

    Why: The star collects every extra positional argument into the tuple prices, which sum() can then add up. A leading * on the parameter name is the whole syntax.

  8. Output predictionq0645

    8. What does the nested function call print?

    def f(x):
        return x * 2
    print(f(f(3)))
    Show answer

    Answer

    12

    Why: The inner call f(3) runs first and yields 6, then the outer call doubles that to 12. Arguments are always evaluated before the call they belong to.

  9. Output predictionq0661

    9. The function has a default argument. What do the two calls return?

    def add_one(x=2):
        return x + 1
    print(add_one(), add_one(5))
    Show answer

    Answer

    3 6

    Why: add_one() falls back to the default 2 and returns 3, while add_one(5) overrides it and returns 6. Defaults apply only when the argument is omitted.

  10. Output predictionq0664

    10. The closure saves the outer variable. What does outer(2)(3) print?

    def outer(x):
        def inner(y):
            return x + y
        return inner
    print(outer(2)(3))
    Show answer

    Answer

    5

    Why: outer(2) returns inner, a closure that remembers x=2, and calling it with 3 gives 5. The closure keeps x alive after outer has already returned.

Common questions

What is the difference between print and return?

print writes text to the console. return sends a value back to the caller so another expression can use it.

Why is a mutable default argument risky?

The default object is created once when the function is defined, so changes can persist across calls. Use None and create a new object inside the function.

Keep the streak going.

The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.

Open full practice