Python Programming - Lambda Functions
Exercise : Lambda Functions - General Questions
- Lambda Functions - General Questions
51.
What does the following lambda function accomplish?
lambda x: x.capitalize() if isinstance(x, str) else None
Answer: Option
Explanation:
The lambda function capitalizes the first letter of a string and returns None for non-string values.
52.
In Python, how can a lambda function be used with the
max()
function to find the maximum length string in a list?
Answer: Option
Explanation:
The
key
parameter is used to specify the lambda function that calculates the length of each string.
53.
When using the
sorted()
function with a lambda function to sort a list of dictionaries, how can you sort based on a specific key within each dictionary?
Answer: Option
Explanation:
The
key
parameter is used to specify the lambda function that extracts the specific key for sorting.
54.
What is the purpose of the
functools.lru_cache()
function when used with a lambda function?
Answer: Option
Explanation:
functools.lru_cache()
is used to create a memoized version of a function, including lambda functions, for efficient caching of results.
55.
What is the result of the following code?
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2 if x % 2 == 0 else x, numbers))
print(squared)
Answer: Option
Explanation:
The lambda function squares even numbers and leaves odd numbers unchanged.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers