Question 1Easy
What does a Python function return if it has no return statement?
- None
- It raises an error
- 0
- An empty tuple
Answer: A. None
Explanation
A function that ends without executing a return statement returns None.
Step-by-step solution
- A function without return finishes and hands back a default value.
- That default is None.
Why the other options are wrong
- B. Python does not raise an error for a missing return.
- C. None is not 0; it is its own type (NoneType).
- D. No tuple is created implicitly.
Tip: Print the result of a no-return function to see None.
Question 2Easy
What is the output?
def f(*args):
return len(args)
print(f(1, 2, 3))
- (1, 2, 3)
- 3
- 1
- Error
Answer: B. 3
Explanation
*args collects the positional arguments into a tuple, here (1, 2, 3), whose length is 3.
Step-by-step solution
- *args gathers all the positional arguments into a tuple.
- The call f(1, 2, 3) gives args = (1, 2, 3).
- len(args) is 3, the correct answer.
Why the other options are wrong
- A. (1, 2, 3) is wrong; the function returns the length, not the tuple.
- C. 1 is wrong; the tuple has three items.
- D. Error is wrong; *args accepts any number of positional arguments.
Tip: *args is a tuple of the extra positional arguments, and kwargs is a dict of keyword arguments.
Question 4Medium
What is the output?
def f(a, b=2, *args, **kw):
print(a, b, args, kw)
f(1, 3, 4, x=5)
- 1 3 (4, 5) {}
- 1 3 (4,) {'x': 5}
- 1 3 [4] {'x': 5}
- 1 2 (3, 4) {'x': 5}
Answer: B. 1 3 (4,) {'x': 5}
Explanation
Positional arguments fill a=1 and b=3, the extra positional 4 goes into args as (4,), and the keyword x=5 goes into kw.
Step-by-step solution
- In f(1, 3, 4, x=5), the first positional value 1 fills a, and the second value 3 fills b.
- The extra positional value 4 goes into args as the one-item tuple (4,).
- x=5 is a keyword argument, so kw is {'x': 5}. The output is 1 3 (4,) {'x': 5}, the correct answer.
Why the other options are wrong
- A. (4, 5) is wrong; 5 is a keyword argument, so it goes into kw, not args.
- C. [4] is wrong; args is a tuple, so it prints as (4,).
- D. 1 2 (3, 4) is wrong; b gets 3, not the default 2, and 3 is not in args.
Tip: Positional arguments fill the parameters in order, *args collects the extra ones, and kw collects keyword arguments.
Question 5Medium
What is the output?
print(list(map(lambda x: x * 2, filter(lambda x: x % 2, range(6)))))
- [0, 4, 8]
- [2, 6, 10]
- [0, 2, 4, 6, 8, 10]
- [1, 3, 5]
Answer: B. [2, 6, 10]
Explanation
filter keeps the odd numbers 1, 3, 5 (x % 2 is truthy for them), and map doubles each one to give 2, 6, 10.
Step-by-step solution
- range(6) gives 0 to 5. filter keeps the values where x % 2 is not 0, which are 1, 3 and 5.
- Map doubles each of them: 2, 6 and 10.
- The output is [2, 6, 10], the correct answer.
Why the other options are wrong
- A. [0, 4, 8] is wrong; filter keeps the odd values 1, 3 and 5, not the even ones.
- C. [0, 2, 4, 6, 8, 10] is wrong; filter removes the even values before map runs.
- D. [1, 3, 5] is wrong; map doubles the values, so they are not 1, 3 and 5.
Tip: Work from the inside out: filter runs first, then map.
Question 6Hard
What is the output?
fs = [lambda: i for i in range(3)]
print([f() for f in fs])
- [0, 1, 2]
- [3, 3, 3]
- [0, 0, 0]
- [2, 2, 2]
Answer: D. [2, 2, 2]
Explanation
Closures capture the variable i, not its value at creation time. By the time the lambdas run, the loop has finished and i is 2. Use lambda i=i: i to capture each value.
Step-by-step solution
- Each lambda refers to the variable i, not to the value i had when the lambda was made.
- The loop ends with i = 2, and all three lambdas read that same i.
- So the output is [2, 2, 2], the correct answer.
Why the other options are wrong
- A. [0, 1, 2] is wrong; the lambdas share one variable, so they do not keep each value.
- B. [3, 3, 3] is wrong; the loop ends at 2, not 3.
- C. [0, 0, 0] is wrong; the final value of i is 2.
Tip: To capture each value, bind it as a default argument, as in lambda i=i: i.
Question 7Hard
What is the output?
def add(x, acc=[]):
acc.append(x)
return acc
add(1)
print(add(2))
- [[1], 2]
- [2]
- [1]
- [1, 2]
Answer: D. [1, 2]
Explanation
Default values are evaluated once, when the function is defined, so every call that omits acc shares the same list. Use acc=None and create the list inside the function.
Step-by-step solution
- The default list acc=[] is created once, when the function is defined.
- add(1) appends 1 to that shared list. add(2) appends 2 to the same list.
- The second call returns [1, 2], the correct answer.
Why the other options are wrong
- A. [[1], 2] is wrong; the function returns one flat list, not nested lists.
- B. [2] is wrong; the list is shared, so the 1 from the first call is still in it.
- C. [1] is wrong; the second call adds 2 to the same list.
Tip: Avoid a mutable default like [] in a function. Use acc=None and create the list inside the function.