
This set examines how Python functions really work. It covers positional, keyword, default, variable-length and keyword-only parameters, the mutable default argument trap, LEGB name resolution with global and nonlocal, closures and late binding, lambda expressions, map, filter and sorted with key functions, and recursion. These are among the most frequently asked topics in Python developer interviews and placement tests, and output-tracing questions on them regularly appear in university examinations.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.
Topics covered in this set
- Positional, keyword and default arguments
- *args, **kwargs and argument unpacking
- Positional-only and keyword-only parameters
- LEGB scope rules, global and nonlocal
- Closures and late binding
- lambda with map, filter and sorted
- Recursion and RecursionError
Python MCQs with answers and explanations
Q1. What is the output of the following code?
def f(a, L=[]):
L.append(a)
return L
f(1)
print(f(2))
a) [2]
b) [1]
c) [[1], 2]
d) [1, 2]
Q2. What is the output of the following code?
def f(*args, **kwargs):
print(len(args), sorted(kwargs))
f(1, 2, x=3, y=4)
a) 2 ['x', 'y']
b) 4 []
c) 2 [3, 4]
d) 2 [('x', 3), ('y', 4)]
Q3. What is the output of the following code?
x = 10
def f():
print(x)
x = 5
f()
a) 10
b) 5
c) UnboundLocalError
d) NameError
Q4. What is the output of the following code?
def outer():
c = 0
def inc():
nonlocal c
c += 1
return c
return inc
g = outer()
g()
g()
print(g())
a) 3
b) 1
c) 0
d) UnboundLocalError
Q5. What is the output of the following code?
fs = [lambda: i for i in range(3)] print([f() for f in fs])
a) [0, 1, 2]
b) [2, 2, 2]
c) [0, 0, 0]
d) NameError
Q6. What is the output of the following code?
print(list(map(lambda x, y: x * y, [1, 2, 3], [4, 5])))
a) [4, 10, 3]
b) [4, 10]
c) [4, 10, 18]
d) TypeError
Q7. What is the output of the following code?
print(list(filter(None, [0, 1, '', 'a', [], [0]])))
a) [0, '', []]
b) [1, 'a']
c) TypeError
d) [1, 'a', [0]]
Q8. What is the output of the following code?
def f(n):
return 1 if n <= 1 else n * f(n - 2)
print(f(7))
a) 5040
b) 105
c) 48
d) 15
Q9. Given the definition below, which call is valid?
def f(a, b, /, c, *, d):
return a + b + c + d
a) f(a=1, b=2, c=3, d=4)
b) f(1, 2, 3, 4)
c) f(1, 2, c=3, d=4)
d) f(1, b=2, c=3, d=4)
Q10. What is the output of the following code?
def f(a, b=2, *args, c=5, **kw):
return a + b + c + sum(args) + len(kw)
print(f(1, 3, 4, 5, d=1))
a) 19
b) 18
c) 15
d) TypeError
Q11. In what order does Python search scopes when resolving an unqualified name inside a nested function?
a) Local, Global, Enclosing, Built-in
b) Built-in, Global, Enclosing, Local
c) Local, Enclosing, Global, Built-in
d) Enclosing, Local, Global, Built-in
Q12. What is the output of the following code?
words = ["bb", "a", "ccc", "dd"] print(sorted(words, key=lambda s: (-len(s), s)))
a) ['a', 'bb', 'dd', 'ccc']
b) ['ccc', 'dd', 'bb', 'a']
c) ['a', 'bb', 'ccc', 'dd']
d) ['ccc', 'bb', 'dd', 'a']
Q13. Which exception is raised when a recursive function exceeds Python's maximum recursion depth?
a) StackOverflowError
b) RecursionError
c) MemoryError
d) OverflowError
Q14. What is the output of the following code?
def add(a, b):
return a + b
args = (1,)
kw = {'b': 5}
print(add(*args, **kw))
a) TypeError
b) 6
c) (1, 5)
d) 15
Q15. What is the output of the following code?
x = 1
def f():
global x
x += 1
return x
print(f() + f(), x)
a) 5 3
b) 4 3
c) 5 1
d) UnboundLocalError
Q16. What is the output of the following code?
print((lambda x: (lambda y: x + y))(3)(4))
a) 3
b) TypeError
c) 12
d) 7
Q17. What is the output of the following code?
def f(**kw):
return kw
print(f(a=1, **{'b': 2}))
a) {'b': 2}
b) TypeError
c) {'a': 1, 'b': 2}
d) {'a': 1}
Q18. Which statement about lambda functions in Python is correct?
a) Its body must be a single expression, whose value is returned automatically
b) It can contain multiple statements separated by semicolons
c) It cannot take default arguments
d) It cannot be assigned to a variable
Quick revision notes
- Default argument values are evaluated once, when the def statement runs.
- Use None as the default for mutable parameters and create the object inside the function.
- Name lookup order is LEGB: Local, Enclosing, Global, Built-in.
- Assigning to a name anywhere in a function makes it local throughout that function.
- global rebinds a module-level name; nonlocal rebinds a name in the nearest enclosing function.
- Parameters before / are positional-only; parameters after * or *args are keyword-only.
- A function without a return statement returns None.
- map() and filter() return lazy iterators in Python 3.
Frequently asked questions
Why is using a list as a default argument a bad idea in Python?
The default list is created only once, when the function is defined, so every call that omits the argument shares and mutates the same list. The usual fix is def f(x, items=None) and then items = [] inside the function when items is None.
What is the difference between global and nonlocal?
global makes a name inside a function refer to the module-level variable. nonlocal makes it refer to a variable in the nearest enclosing function scope, which is how closures update state such as a counter.
What is late binding in Python closures?
A closure looks up the value of a free variable when it is called, not when it is created. Lambdas created in a loop therefore all see the loop variable's final value unless it is captured with a default argument such as lambda i=i: i.
Comments
Post a Comment