Python Lambda Functions Quiz
A 35-question quiz covering anonymous functions, their syntax, usage with map/filter/sort, and their limitations.
Question 1
What is a lambda function in Python?
Question 2
Analyze the code below. What is the output?
square = lambda x: x * x
print(square(5))Question 3
What is the correct syntax for a lambda function?
Question 4
Analyze the code below. What does `result` contain?
nums = [1, 2, 3, 4]
result = list(map(lambda x: x * 2, nums))Question 5
Can a lambda function contain multiple expressions or statements?
Question 6
Analyze the code below. What does `result` contain?
nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, nums))Question 7
How do you use a lambda to sort a list of tuples `[(1, 2), (3, 1), (5, 0)]` by the second element?
Question 8
Analyze the code below. What is the output?
func = lambda a, b: a + b
print(func(3, 4))Question 9
Which of the following is NOT allowed inside a lambda?
Question 10
Analyze the code below. What is the output?
points = [{'x': 1, 'y': 2}, {'x': 3, 'y': 1}]
points.sort(key=lambda p: p['y'])
print(points[0]['x'])Question 11
What is the return type of a lambda expression?
Question 12
Analyze the code below. What is `x`?
x = (lambda: "Hello")()Question 13
Can you use type hints/annotations directly in a lambda's argument list?
Question 14
Analyze the code below. What is the output?
funcs = [lambda x: x + i for i in range(3)]
print(funcs[0](10))Question 15
What is the equivalent `def` function for `lambda x, y: x + y`?
Question 16
Analyze the code below. What does `reduce` do here?
from functools import reduce
res = reduce(lambda x, y: x * y, [1, 2, 3, 4])Question 17
Why might PEP 8 discourage assigning lambdas to variables (e.g., `f = lambda x: x`)?
Question 18
Analyze the code below. What is the output?
full_name = lambda first, last: f"{first.title()} {last.title()}"
print(full_name("john", "doe"))Question 19
Can a lambda function take `*args` and `**kwargs`?
Question 20
Analyze the code below. What is the output?
nums = [1, 2, 3, 4]
print(list(filter(lambda x: x > 2, nums)))Question 21
Which of these is a valid use case for a lambda?
Question 22
Analyze the code below. What is the result?
(lambda x: x + 1)(2)Question 23
Can a lambda function access variables from the surrounding scope?
Question 24
Analyze the code below. What is `res`?
res = (lambda x, y=10: x + y)(5)Question 25
What is the primary limitation of lambda functions compared to `def`?
Question 26
Analyze the code below. What is the output?
mx = lambda a, b: a if a > b else b
print(mx(10, 5))Question 27
Can you use a lambda as a method inside a class?
Question 28
Analyze the code below. What is the output?
l = [1, 2, 3, 4]
l.sort(key=lambda x: -x)
print(l)Question 29
Which built-in function is often used with lambda to check if any element in a list satisfies a condition?
Question 30
Analyze the code below. What is `val`?
val = (lambda *args: sum(args))(1, 2, 3)Question 31
Can a lambda function have a docstring?
Question 32
Analyze the code below. What is the output?
pairs = [(1, 'one'), (2, 'two'), (3, 'three')]
pairs.sort(key=lambda pair: len(pair[1]))
print(pairs[0][0])Question 33
Is `lambda: None` a valid function?
Question 34
Analyze the code below. What is the output?
act = lambda x: print(x)
res = act("Hello")
print(res)Question 35
Why are lambdas often used with `key` arguments in sorting?
