What is a lambda function in Python?
A
A function with a loop B
A function with no return value C
A small anonymous function D
A class method
Analysis & Theory
A lambda function is an anonymous (unnamed) function defined using the `lambda` keyword.
Which keyword is used to create a lambda function?
A
def B
function C
lambda D
anonymous
Analysis & Theory
Lambda functions are defined using the `lambda` keyword.
What is the output of this code?
x = lambda a: a + 5
print(x(3))
A
8 B
5 C
3 D
Error
Analysis & Theory
`lambda a: a + 5` creates a function that returns 3 + 5 = 8.
What is the correct syntax of a lambda function that adds two numbers?
A
lambda a + b: return a + b B
def lambda(a, b): return a + b C
lambda a, b: a + b D
lambda a, b return a + b
Analysis & Theory
Correct syntax: `lambda a, b: a + b` — the result is returned automatically.
Can a lambda function have multiple expressions?
A
Yes B
No, only one expression C
Only if using `def` D
Only with loops
Analysis & Theory
Lambda functions can only contain one expression, not multiple lines or statements.
Which of the following is equivalent to `lambda x: x * x`?
A
def square(x): return x * x B
x = lambda x: x + x C
lambda x: x + 1 D
def lambda(x): x * x
Analysis & Theory
`lambda x: x * x` is equivalent to a function that returns the square of `x`.
What is the output?
print((lambda x, y: x * y)(2, 4))
A
6 B
8 C
24 D
2 * y
Analysis & Theory
The lambda function multiplies 2 and 4 and returns 8.
Where are lambda functions commonly used?
A
With classes B
In decorators C
With functions like `map()`, `filter()`, and `sorted()` D
To create classes
Analysis & Theory
Lambda functions are often used as quick, inline functions in `map()`, `filter()`, and `sorted()`.
What will this return?
nums = [1, 2, 3, 4]
print(list(map(lambda x: x * 2, nums)))
A
[2, 4, 6, 8] B
[1, 2, 3, 4] C
[4, 6, 8] D
Error
Analysis & Theory
`map()` applies the lambda to each item, doubling the values.
What is the result?
list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4]))
A
[1, 3] B
[2, 4] C
[1, 2, 3, 4] D
[]
Analysis & Theory
`filter()` keeps only even numbers where `x % 2 == 0` is True.