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
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming writes a function that prints a menu but has no return statement. What does a call return?
Show answer
Answer
C. NoneWhy: 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. 🧋 Ming writes a menu-printing function. What is the return value of `def f(): pass`?
Show answer
Answer
A. NoneWhy: 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. 🧋 What does this code print? count=0 def f(): global count count+=1 f() print(count)
Show answer
Answer
B. 1Why: 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. What does a function with no return statement return?
Show answer
Answer
C. NoneWhy: 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. A temporary variable defined inside a function, such as one drink's ingredients, is called a ___ variable.
Show answer
Answer
localWhy: 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. 🧋 A variable defined inside a function is called a ___ variable.
Show answer
Answer
localWhy: 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. Accept any number of positional arguments. What goes in the blank?
def total(*____): return sum(prices)Show answer
Answer
pricesWhy: 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. What does the nested function call print?
def f(x): return x * 2 print(f(f(3)))Show answer
Answer
12Why: 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. 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 6Why: 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. 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
5Why: 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.