Exercise: Python

Questions for: Lambda Functions

What happens if a lambda function has more parameters than arguments provided during its invocation?
A:
It raises a SyntaxError.
B:
The extra parameters are ignored.
C:
It raises a TypeError.
D:
The lambda function automatically assigns default values to the extra parameters.
Answer: C
If a lambda function is defined with more parameters than provided during invocation, a TypeError is raised.
How is a lambda function different from a regular function in terms of defining and using it?
A:
Lambda functions are defined using the keyword lambda and can be used immediately, while regular functions are defined with def.
B:
Regular functions are defined using the keyword lambda and can be used immediately, while lambda functions are defined with def.
C:
Both lambda and regular functions are defined using the keyword func and can be used interchangeably.
D:
Lambda functions are defined using the keyword def and require parentheses around parameters, while regular functions use lambda without parentheses.
Answer: A
Lambda functions are defined using the lambda keyword and are typically used for short-term, specific tasks without the need for a formal function definition.
Which built-in function can be used to find the minimum value in an iterable using a lambda function?
A:
sum()
B:
min()
C:
reduce()
D:
filter()
Answer: B
The min() function can be used with a lambda function to find the minimum value in an iterable based on the lambda function's criteria.
What is the result of the following Python code?
my_list = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, my_list))
print(result)
A:
[1, 2, 3, 4, 5]
B:
[2, 4, 6, 8, 10]
C:
[1, 4, 9, 16, 25]
D:
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Answer: B
The map() function applies the lambda function lambda x: x * 2 to each element of my_list, doubling each value.
What is the purpose of the following code using the enumerate() function and a lambda function?
my_list = [1, 2, 3, 4, 5]
result = list(map(lambda x: x[0] * x[1], enumerate(my_list)))
A:
To apply the lambda function to each element of an iterable
B:
To filter elements from an iterable based on the lambda function's condition
C:
To create a sorted list based on the lambda function's values
D:
To create a list of products of each element and its index in the iterable
Answer: D
The code uses enumerate() to get tuples of index and element, and the lambda function multiplies the index (x[0]) by the element (x[1]).
Ad Slot (Above Pagination)
Quiz