Exercise: Lambda Functions

Questions for: Lambda Functions

Which of the following built-in functions can be used to find the length of a list of strings using a lambda function?
A:
len()
B:
map()
C:
reduce()
D:
filter()
Answer: A
The len() function can be used with a lambda function to find the length of a list of strings.
What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)
A:
[2, 4]
B:
[1, 3, 5]
C:
[1, 2, 3, 4, 5]
D:
[1, 4]
Answer: A
The filter() function with the lambda filters even numbers from the list.
What does the following lambda function accomplish?
lambda x: x.split()[0]
A:
Splits a string into a list.
B:
Extracts the first word from a string.
C:
Concatenates two strings.
D:
Reverses a string.
Answer: B
The lambda function extracts the first word from a string using the split() method.
How is a lambda function different from a regular function?
A:
Lambda functions can have multiple expressions.
B:
Regular functions are always more memory-efficient.
C:
Lambda functions can have named parameters.
D:
Regular functions cannot be used as arguments in higher-order functions.
Answer: A
Lambda functions are limited to a single expression, making them concise for short tasks.
What is the purpose of the functools.partial() function in relation to lambda functions?
A:
To create a partial application of a lambda function.
B:
To create a filter object based on the lambda function's condition.
C:
To sort the elements of an iterable using the lambda function.
D:
To apply the lambda function to each element of an iterable.
Answer: A
functools.partial() is used to create a partial application of a function, including lambda functions, by fixing certain arguments.
Ad Slot (Above Pagination)
Quiz