Sample questions
Question 1Easy
What is printed by print(print("A"))?
- A None on the same line
- A on one line, then None on the next
- A only
- None only
Answer: B. A on one line, then None on the next
Explanation
The inner print outputs A and returns None; the outer print then prints that None on a new line.
Step-by-step solution
- The inner print("A") writes A and a newline, then returns None.
- The outer print receives None and writes None on the next line.
- So the output is A, then None on the next line, which is option B.
Why the other options are wrong
- A. Print always ends with a newline, so A and None are on separate lines.
- C. The outer print also runs and prints None.
- D. The inner print also runs and prints A first.
Tip: Print returns None, so nesting print inside print shows None.
Question 2Easy
What is the output?
def swap(a, b):
return b, a
x, y = swap(1, 2)
print(x, y)
- 2 1
- 1 2
- TypeError
- (2, 1)
Answer: A. 2 1
Explanation
The function returns the tuple (2, 1), which is unpacked into x and y.
Step-by-step solution
- swap(1, 2) returns the tuple (2, 1), with b first and a second.
- X, y = (2, 1) unpacks the tuple so x is 2 and y is 1.
- print(x, y) shows 2 1, which is option A.
Why the other options are wrong
- B. The function swaps the values, so x gets 2, not 1.
- C. Unpacking a 2-item tuple into two names works; no TypeError happens.
- D. The tuple is unpacked, so the output is 2 1, not (2, 1).
Tip: Return a, b gives a tuple, and x, y = ... unpacks it.
Question 3Easy
What is the type of args inside def f(*args): ?
- list
- tuple
- set
- dict
Answer: B. tuple
Explanation
Extra positional arguments are packed into a tuple; extra keyword arguments (**kwargs) are packed into a dict.
Step-by-step solution
- In def f(*args), the star packs all extra positional arguments into one object.
- Python packs them into a tuple, even when no argument is passed (it is then an empty tuple).
- So the type of args is tuple, which is option B.
Why the other options are wrong
- A. The packed object is a tuple, not a list; lists are not created by *args.
- C. A set is unordered and is never created by *args.
- D. A dict is created by kwargs, not by *args.
Tip: *args gives a tuple and kwargs gives a dict.
Question 4Easy
What is the output?
def f(n):
if n > 0:
return "positive"
print(f(5), f(-5))
- positive None
- positive
- positive negative
- positive False
Answer: A. positive None
Explanation
When no return statement is reached the function returns None, which print shows as None.
Step-by-step solution
- f(5): n is greater than 0, so the function returns "positive".
- f(-5): the if is false and there is no other return, so the function returns None.
- Print shows the two values separated by a space: positive None.
- So the correct answer is option A.
Why the other options are wrong
- B. The second call returns None, which print still shows, so the output has two values.
- C. f(-5) does not return the string "negative"; no such return exists.
- D. f(-5) returns None, not False.
Tip: A function with no return (or a path without one) returns None.
Question 5Medium
What is the output?
def f(*, key):
return key * 2
try:
print(f(5))
except TypeError:
print("err", f(key=5))
- 10
- 10 10
- err 10
- err 5
Answer: C. err 10
Explanation
A bare * makes every following parameter keyword-only, so f(5) raises TypeError; f(key=5) returns 10.
Step-by-step solution
- The bare * makes key keyword-only.
- f(5) passes 5 by position, which is not allowed, so it raises TypeError.
- The except block prints err, then calls f(key=5), which gives 10.
- The output is err 10.
Why the other options are wrong
- A. f(5) raises TypeError before any print happens.
- B. The first call fails, so only one 10 is printed, after err.
- D. f(key=5) returns key times 2, which is 10, not 5.
Tip: A bare * in a signature means every parameter after it must be passed by keyword.
Question 6Medium
What is the output?
def fact(n):
return 1 if n <= 1 else n * fact(n - 1)
print(fact(5), fact(0))
- 120 0
- 720 1
- 24 1
- 120 1
Answer: D. 120 1
Explanation
fact(5) = 5*4*3*2*1 = 120, and the base case returns 1 for n <= 1, including 0.
Step-by-step solution
fact(5) = 5*4*3*2*1 = 120.fact(0): n <= 1 returns 1.- Output 120 1.
Why the other options are wrong
- A. 0! = 1.
- B. That is 6!.
- C. That is 4!.
Tip: 0! = 1.
Question 7Medium
What happens when this code runs?
def f(a, b):
return a + b
print(f(1, a=2))
SyntaxError- 3 is printed
TypeError: f() got multiple values for argument aTypeError: missing argument b
Answer: C. TypeError: f() got multiple values for argument a
Explanation
The positional 1 is already bound to a, and a=2 tries to bind a again, so Python raises TypeError for multiple values.
Step-by-step solution
- The first positional argument 1 is bound to a.
- The keyword a=2 tries to bind a again.
- Python sees a parameter given twice and raises TypeError.
- The error is TypeError: f() got multiple values for argument a.
Why the other options are wrong
- A. The code is valid syntax; the problem is at run time.
- B. The call fails before the addition, so 3 is never printed.
- D. b is indeed not passed, but Python reports the repeated a first, so the error is about a being given twice.
Tip: Each parameter can receive only one value, by position or by keyword.
Question 8Medium
What is the output?
def outer(**kwargs):
return sorted(kwargs.items())
print(outer(b=2, a=1))
- [('a', 1), ('b', 2)]
- [('b', 2), ('a', 1)]
- ['a', 'b']
- {'a': 1, 'b': 2}
Answer: A. [('a', 1), ('b', 2)]
Explanation
**kwargs collects keyword arguments into a dict; items() gives (key, value) tuples and sorted orders them by key.
Step-by-step solution
- Kwargs becomes the dict with a 1 and b 2.
- items() gives the pairs ('b', 2) and ('a', 1).
- Sorted orders them by key, so a comes first.
- The output is [('a', 1), ('b', 2)].
Why the other options are wrong
- B. Sorted puts a before b, so the pairs are not in the original order.
- C. Items gives pairs, not just the keys.
- D. Sorted returns a list of pairs, not a dict.
Tip: Sorted on dict items returns a list of (key, value) tuples ordered by key.
Question 9Hard
What is the output?
def compose(*fns):
def run(x):
for fn in reversed(fns):
x = fn(x)
return x
return run
inc = lambda x: x + 1
dbl = lambda x: x * 2
print(compose(inc, dbl)(5), compose(dbl, inc)(5))
- 12 12
- 11 11
- 12 11
- 11 12
Answer: D. 11 12
Explanation
compose applies the functions right to left:
compose(inc, dbl)(5) = inc(dbl(5)) = 11, and compose(dbl, inc)(5) = dbl(inc(5)) = 12.
Step-by-step solution
- compose(inc, dbl) runs its functions in reverse, so dbl runs first, then inc.
- compose(inc, dbl)(5) is inc(dbl(5)) = inc(10) = 11.
- compose(dbl, inc)(5) is dbl(inc(5)) = dbl(6) = 12.
- The output is 11 12, which is option D.
Why the other options are wrong
- A. The first call runs dbl first and then inc, which gives 11, not 12.
- B. The second call gives 12, since dbl(inc(5)) = 12.
- C. The first call is inc(dbl(5)) = 11, not 12.
Tip: With reversed(fns), the last function in the call is applied first.
Question 10Hard
What is the output?
def stamp(n, seen=len("abc")):
return n + seen
print(stamp(1), stamp(1, 0))
- TypeError
- 4 1
- 4 4
- 1 1
Answer: B. 4 1
Explanation
Default expressions are evaluated once, when the def statement runs; len("abc") is 3, so stamp(1) is 4 and stamp(1, 0) is 1.
Step-by-step solution
- The default seen is len of abc, which is 3, evaluated once when the def runs.
- stamp(1) uses the default: 1 + 3 = 4.
- stamp(1, 0) passes 0 for seen: 1 + 0 = 1.
- The output is 4 1.
Why the other options are wrong
- A. The default is a valid expression, so no TypeError is raised.
- C. stamp(1, 0) passes 0, so it gives 1, not 4.
- D. stamp(1) uses the default 3, so it gives 4, not 1.
Tip: Default values are evaluated once, when the function is defined.
Practise all 32 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →