Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
11.
In Python, what is the purpose of the sorted() function when used with a lambda function?
To apply the lambda function to each element of an iterable
To create a sorted list based on the lambda function's values
To filter elements from an iterable based on the lambda function's condition
To map the lambda function to each element of an iterable
Answer: Option
Explanation:
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.

12.
What happens if a lambda function is used as an argument for the key parameter in the max() function?
It raises a SyntaxError
It serves as the comparison function for finding the maximum value
It must be enclosed in square brackets
It only works if the lambda function has a single parameter
Answer: Option
Explanation:
When a lambda function is used as the key parameter in max(), it defines the sorting criteria for finding the maximum value.

13.
How does the scope of variables work in lambda functions?
Lambda functions can only access global variables
Lambda functions have their own local scope
Lambda functions can access variables from the calling environment
Lambda functions cannot access any variables
Answer: Option
Explanation:
Lambda functions can access variables from the surrounding environment, making them useful for short, specific tasks.

14.
What is the purpose of the any() function when used with a lambda function?
To apply the lambda function to each element of an iterable
To check if any element in an iterable satisfies the lambda function's condition
To create a filter based on the lambda function's condition
To map the lambda function to each element of an iterable
Answer: Option
Explanation:
The any() function returns True if at least one element in the iterable satisfies the condition specified by the lambda function.

15.
Which of the following is a valid reason for using a regular function instead of a lambda function?
When defining a function for a short, one-time task
When the function requires multiple expressions or statements
When the function is passed as an argument to map()
When the function is used for filtering elements based on a condition
Answer: Option
Explanation:
Regular functions are more suitable when complex logic, multiple expressions, or statements are needed, as they can span multiple lines of code.