Python Lambda Functions Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering anonymous functions, their syntax, usage with map/filter/sort, and their limitations.

35 Questions
~70 minutes
1

Question 1

What is a lambda function in Python?

A
A small, anonymous function defined with the `lambda` keyword.
B
A function that runs in the cloud.
C
A function that can only be used once.
D
A recursive function.
2

Question 2

Analyze the code below. What is the output?

python
square = lambda x: x * x
print(square(5))
A
25
B
10
C
5
D
Error
3

Question 3

What is the correct syntax for a lambda function?

A
lambda arguments: expression
B
lambda arguments -> expression
C
def lambda(arguments): expression
D
expression lambda arguments
4

Question 4

Analyze the code below. What does `result` contain?

python
nums = [1, 2, 3, 4]
result = list(map(lambda x: x * 2, nums))
A
[2, 4, 6, 8]
B
[1, 2, 3, 4]
C
[1, 4, 9, 16]
D
Error
5

Question 5

Can a lambda function contain multiple expressions or statements?

A
No, it is restricted to a single expression.
B
Yes, if separated by semicolons.
C
Yes, if enclosed in braces.
D
Yes, unlimited lines.
6

Question 6

Analyze the code below. What does `result` contain?

python
nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, nums))
A
[2, 4]
B
[1, 3, 5]
C
[False, True, False, True, False]
D
[1, 2, 3, 4, 5]
7

Question 7

How do you use a lambda to sort a list of tuples `[(1, 2), (3, 1), (5, 0)]` by the second element?

A
sorted(lst, key=lambda x: x[1])
B
sorted(lst, key=lambda x: x[0])
C
sorted(lst, lambda x: x[1])
D
lst.sort(key=1)
8

Question 8

Analyze the code below. What is the output?

python
func = lambda a, b: a + b
print(func(3, 4))
A
7
B
34
C
12
D
Error
9

Question 9

Which of the following is NOT allowed inside a lambda?

A
Assignment statements (e.g., `x = 5`).
B
Function calls.
C
Conditional expressions (ternary operator).
D
Mathematical operations.
10

Question 10

Analyze the code below. What is the output?

python
points = [{'x': 1, 'y': 2}, {'x': 3, 'y': 1}]
points.sort(key=lambda p: p['y'])
print(points[0]['x'])
A
3
B
1
C
2
D
Error
11

Question 11

What is the return type of a lambda expression?

A
A function object.
B
The result of the expression immediately.
C
None.
D
A string.
12

Question 12

Analyze the code below. What is `x`?

python
x = (lambda: "Hello")()
A
'Hello'
B
A function object.
C
None
D
Error
13

Question 13

Can you use type hints/annotations directly in a lambda's argument list?

A
No, syntax does not support it.
B
Yes, e.g., lambda x: int: x
C
Yes, but only for return types.
D
Yes, fully supported.
14

Question 14

Analyze the code below. What is the output?

python
funcs = [lambda x: x + i for i in range(3)]
print(funcs[0](10))
A
12
B
10
C
11
D
Error
15

Question 15

What is the equivalent `def` function for `lambda x, y: x + y`?

A
def func(x, y): return x + y
B
def func(x, y): print(x + y)
C
def func(x, y): x + y
D
def func(x): return y
16

Question 16

Analyze the code below. What does `reduce` do here?

python
from functools import reduce
res = reduce(lambda x, y: x * y, [1, 2, 3, 4])
A
Calculates the product: 24.
B
Calculates the sum: 10.
C
Returns the last element: 4.
D
Returns a list.
17

Question 17

Why might PEP 8 discourage assigning lambdas to variables (e.g., `f = lambda x: x`)?

A
It reduces clarity and traceback readability compared to `def`.
B
It is slower.
C
It is a syntax error.
D
It uses more memory.
18

Question 18

Analyze the code below. What is the output?

python
full_name = lambda first, last: f"{first.title()} {last.title()}"
print(full_name("john", "doe"))
A
John Doe
B
john doe
C
JOHN DOE
D
John doe
19

Question 19

Can a lambda function take `*args` and `**kwargs`?

A
Yes.
B
No.
C
Only *args.
D
Only **kwargs.
20

Question 20

Analyze the code below. What is the output?

python
nums = [1, 2, 3, 4]
print(list(filter(lambda x: x > 2, nums)))
A
[3, 4]
B
[1, 2]
C
[True, True]
D
[1, 2, 3, 4]
21

Question 21

Which of these is a valid use case for a lambda?

A
Passing a small callback function to a GUI button.
B
Writing a complex data processing pipeline.
C
Defining a class method with docstrings.
D
Declaring global constants.
22

Question 22

Analyze the code below. What is the result?

python
(lambda x: x + 1)(2)
A
3
B
2
C
Function object
D
Syntax Error
23

Question 23

Can a lambda function access variables from the surrounding scope?

A
Yes, they are closures.
B
No, only arguments.
C
Only global variables.
D
Only if declared global.
24

Question 24

Analyze the code below. What is `res`?

python
res = (lambda x, y=10: x + y)(5)
A
15
B
5
C
Error
D
10
25

Question 25

What is the primary limitation of lambda functions compared to `def`?

A
They can only contain a single expression.
B
They cannot take arguments.
C
They are slower.
D
They cannot return values.
26

Question 26

Analyze the code below. What is the output?

python
mx = lambda a, b: a if a > b else b
print(mx(10, 5))
A
10
B
5
C
True
D
Error
27

Question 27

Can you use a lambda as a method inside a class?

A
Yes, but it's generally considered bad style.
B
No, it raises an error.
C
Yes, it is the preferred way.
D
Only for static methods.
28

Question 28

Analyze the code below. What is the output?

python
l = [1, 2, 3, 4]
l.sort(key=lambda x: -x)
print(l)
A
[4, 3, 2, 1]
B
[1, 2, 3, 4]
C
[-4, -3, -2, -1]
D
Error
29

Question 29

Which built-in function is often used with lambda to check if any element in a list satisfies a condition?

A
any(map(lambda ...))
B
check(lambda ...)
C
exists(lambda ...)
D
some(lambda ...)
30

Question 30

Analyze the code below. What is `val`?

python
val = (lambda *args: sum(args))(1, 2, 3)
A
6
B
[1, 2, 3]
C
1
D
Error
31

Question 31

Can a lambda function have a docstring?

A
No.
B
Yes, as the first statement.
C
Yes, using __doc__ assignment.
D
Yes, automatically generated.
32

Question 32

Analyze the code below. What is the output?

python
pairs = [(1, 'one'), (2, 'two'), (3, 'three')]
pairs.sort(key=lambda pair: len(pair[1]))
print(pairs[0][0])
A
1
B
2
C
3
D
Error
33

Question 33

Is `lambda: None` a valid function?

A
Yes, it returns None.
B
No, it must return a value.
C
No, it must have arguments.
D
No, syntax error.
34

Question 34

Analyze the code below. What is the output?

python
act = lambda x: print(x)
res = act("Hello")
print(res)
A
Hello then None
B
Hello then Hello
C
None
D
Error
35

Question 35

Why are lambdas often used with `key` arguments in sorting?

A
They allow defining a custom sort order inline without creating a separate named function.
B
They are faster than `def` functions.
C
They are required by the `sort` method.
D
They automatically sort in reverse.

QUIZZES IN Python