Exercise: Lambda Functions

Questions for: Lambda Functions

In Python, what is the primary difference between a lambda function and a regular function?
A:
Lambda functions cannot take parameters.
B:
Regular functions cannot be used as arguments in higher-order functions.
C:
Lambda functions are defined with the "def" keyword.
D:
Lambda functions are concise and do not require a formal definition.
Answer: D
Lambda functions are often used for short, specific tasks due to their concise syntax and lack of a formal definition.
What is the significance of the "lambda" keyword in Python lambda functions?
A:
It stands for "list and map data."
B:
It denotes the creation of anonymous functions.
C:
It signifies "logical and."
D:
It indicates "length" of a function.
Answer: B
The "lambda" keyword is used to create anonymous functions in Python.
Which built-in function can be used with a lambda function to find the maximum value in an iterable?
A:
min()
B:
max()
C:
sum()
D:
filter()
Answer: B
The max() function can be used with a lambda function to find the maximum value in an iterable based on the lambda function's criteria.
What does the following lambda function do?
lambda x, y: x if x > y else y
A:
Squares the input.
B:
Returns the sum of x and y.
C:
Returns the larger of the two values.
D:
Multiplies x and y.
Answer: C
The lambda function returns the larger value between x and y using a conditional expression.
What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared)
A:
[1, 2, 3, 4, 5]
B:
[1, 4, 9, 16, 25]
C:
[2, 4, 6, 8, 10]
D:
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Answer: B
The map() function applies the lambda function to each element of the list, squaring each number.
Ad Slot (Above Pagination)
Quiz