Exercise: Lambda Functions

Questions for: Lambda Functions

In Python, what is the purpose of the sorted() function when used with a lambda function?
A:
To apply the lambda function to each element of an iterable
B:
To create a sorted list based on the lambda function's values
C:
To filter elements from an iterable based on the lambda function's condition
D:
To map the lambda function to each element of an iterable
Answer: B
The sorted() function is used to create a sorted list from the elements of an iterable, with the sorting criteria defined by the lambda function.
Which of the following is a valid use case for lambda functions?
A:
Defining complex algorithms with multiple statements
B:
Creating short, throwaway functions for a specific task
C:
Implementing class methods in Python
D:
Serving as a replacement for regular functions in all scenarios
Answer: B
Lambda functions are suitable for short-term, specific tasks where a full function definition is not necessary. They are often used for quick, one-time operations.
In Python, can a lambda function have more than one line of code?
A:
Yes, as long as each line is indented
B:
No, lambda functions are limited to a single line
C:
Yes, but only if the lines are separated by commas
D:
No, lambda functions can only contain a single expression
Answer: B
Lambda functions in Python are restricted to a single expression and cannot span multiple lines of code.
What is the purpose of the reduce() function when used with lambda functions?
A:
To create a filter based on a condition
B:
To apply the lambda function to each element of an iterable
C:
To accumulate values from an iterable using the lambda function
D:
To sort the elements of an iterable using the lambda function
Answer: C
The reduce() function is used to accumulate values from an iterable using the lambda function. It successively applies the lambda function to the elements of the iterable.
What is the main difference between map() and filter() functions when used with lambda functions?
A:
map() applies the lambda function to each element, filter() creates a filter based on a condition
B:
map() creates a filter, filter() applies the lambda function to each element
C:
Both functions create filters based on conditions
D:
Both functions apply the lambda function to each element
Answer: A
map() applies the lambda function to each element of an iterable, while filter() creates a filter object based on a condition specified by the lambda function.
Ad Slot (Above Pagination)
Quiz