30+ MCQs on Python Lambda Functions

Ayushi Trivedi Last Updated : 23 Jul, 2024
8 min read

Welcome to the Python Lambda Function Python Interview Questions! Lambda functions, also known as anonymous functions, are concise and powerful tools in Python for creating small, inline functions. They are often used in scenarios where a small function is needed for a short period of time and a full function definition would be cumbersome. These questions will test your understanding of lambda functions in Python, including their syntax, usage, benefits, and limitations. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python lambda functions together!

30+ Python Interview Questions on Python Lambda Function

Q1. What is a lambda function in Python?

a) A built-in function for performing mathematical calculations

b) A function that is defined with the def keyword

c) An anonymous function defined using the lambda keyword

d) A function that can only be used with strings

Answer: c

Explanation: Lambda functions in Python are anonymous functions defined with the lambda keyword.

Q2. Which of the following statements is true about lambda functions?

a) Lambda functions can have multiple expressions

b) Lambda functions can have default arguments

c) Lambda functions cannot have return statements

d) Lambda functions can be named and reused like regular functions

Answer: b

Explanation: Lambda functions can have default arguments, allowing flexibility in their usage.

Q3. What is the syntax for a lambda function in Python?

a) lambda arg1, arg2: expression

b) def lambda(arg1, arg2): expression

c) lambda function(arg1, arg2): expression

d) function(arg1, arg2): expression

Answer: a

Explanation: The syntax for a lambda function is lambda arguments: expression.

Q4. When are lambda functions typically used in Python?

a) To define large, complex functions

b) To create anonymous functions for simple tasks

c) To replace built-in functions like print() and input()

d) To handle exceptions in code

Answer: b

Explanation: Lambda functions are commonly used for creating small, anonymous functions for simple tasks.

Q5. Which of the following is a valid use case for a lambda function?

a) Sorting a list of tuples by the second element

b) Defining a class method

c) Handling file I/O operations

d) Performing matrix multiplication

Answer: a

Explanation: Lambda functions are often used for sorting, especially when the sorting key is based on a specific element in a tuple or object.

Q6. What is the output of the following code?

result = (lambda x: x * 2)(5)
print(result)

a) 5

b) 10

c) 25

d) 15

Answer: b

Explanation: The lambda function (lambda x: x * 2) multiplies the input 5 by 2, resulting in 10.

Q7. Which of the following is true about lambda functions?

a) They cannot have multiple parameters

b) They are always named

c) They can be used to create recursive functions

d) They can contain multiple lines of code

Answer: a

Explanation: Lambda functions are limited to a single expression, so they typically have a single parameter.

Q8. What is the purpose of using lambda functions with map() and filter() functions in Python?

a) To apply the lambda function to all elements of an iterable

b) To remove all elements from an iterable

c) To sort the elements of an iterable

d) To create a new iterable with selected elements

Answer: a

Explanation: Lambda functions are often used with map() and filter() to apply a function to all elements of an iterable and filter elements based on a condition, respectively.

Q9. What will be the output of the following code?

nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * x, nums))
print(result)

a) [1, 2, 3, 4, 5]

b) [1, 4, 9, 16, 25]

c) [2, 4, 6, 8, 10]

d) [0, 1, 2, 3, 4]

Answer: b

Explanation: The map() function applies the lambda function to each element in nums, resulting in a new list with squared values.

Q10. What is the output of the following code?

nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, nums))
print(result)

a) [1, 3, 5]

b) [2, 4]

c) [1, 2, 3, 4, 5]

d) []

Answer: b

Explanation: The filter() function creates a new list with elements that satisfy the condition (even numbers) given by the lambda function.

Q11. What will the following code snippet output?

f = lambda x, y: x + y
result = f(2, 3)
print(result)

a) 2

b) 3

c) 5

d) 6

Answer: c

Explanation: The lambda function f adds its two arguments x and y, so f(2, 3) results in 2 + 3 = 5.

Q12. Which of the following is a correct lambda function to calculate the square of a number?

a) lambda x: x * x

b) lambda x: x ^ 2

c) lambda x: x ** 2

d) lambda x: x + x

Answer: c

Explanation: The lambda function lambda x: x ** 2 calculates the square of a number x.

Q13. What does the following lambda function do?

f = lambda x: x if x % 2 == 0 else None
result = f(5)
print(result)

a) Returns the number if it is even, otherwise None

b) Returns None if the number is even, otherwise the number

c) Returns the square of the number if it is even, otherwise None

d) Returns the number if it is odd, otherwise None

Answer: b

Explanation: The lambda function lambda x: x if x % 2 == 0 else None returns None if the input number x is even, otherwise it returns the number itself.

Q14. What will be the output of the following code?

greet = lambda: "Hello, World!"
result = greet()
print(result)

a) “Hello, World!”

b) “Hello!”

c) None

d) Error: lambda functions require arguments

Answer: a

Explanation: The lambda function greet has no arguments and returns the string “Hello, World!”, which is then printed.

Q15. What is the output of the following code?

func_list = [lambda x: x + 2, lambda x: x * 2, lambda x: x ** 2]
results = [func(5) for func in func_list]
print(results)

a) [7, 10, 25]

b) [10, 25, 7]

c) [7, 10, 5]

d) [10, 5, 25]

Answer: a

Explanation: Each lambda function in func_list is applied to the value 5, resulting in [5 + 2, 5 * 2, 5 ** 2].

Q16. Which of the following is a correct use of a lambda function with multiple arguments?

a) lambda x, y: x * y

b) lambda (x, y): x * y

c) lambda x y: x * y

d) lambda x: x * y

Answer: a

Explanation: The correct syntax for a lambda function with multiple arguments is lambda x, y: x * y.

Q17. What does the following lambda function do?

square_if_positive = lambda x: x ** 2 if x > 0 else None
result = square_if_positive(-3)
print(result)

a) Returns the square of a positive number, otherwise None

b) Returns None if the number is positive, otherwise the square of the number

c) Returns the square of the number if it is positive, otherwise None

d) Returns the number if it is positive, otherwise its square

Answer: c

Explanation: The lambda function square_if_positive returns the square of the number if it is positive, otherwise it returns None.

Q18. What is the output of the following code?

nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x % 2 == 0, nums))
print(result)

a) [False, True, False, True, False]

b) [1, 0, 1, 0, 1]

c) [True, False, True, False, True]

d) [0, 1, 0, 1, 0]

Answer: a

Explanation: The map() function applies the lambda function to each element in nums, resulting in a list of Boolean values indicating whether each element is even.

Q19. Which of the following is a valid lambda function to check if a number is negative?

a) lambda x: x < 0

b) lambda x: x > 0

c) lambda x: x == 0

d) lambda x: x != 0

Answer: a

Explanation: The lambda function lambda x: x < 0 checks if a number is negative.

Q20. What will the following code snippet output?

f = lambda x: "even" if x % 2 == 0 else "odd"
result = f(7)
print(result)

a) “even”

b) “odd”

c) 7

d) None

Answer: b

Explanation: The lambda function f checks if a number is even or odd and returns the respective string.

Q21. What does the following lambda function do?

double_or_square = lambda x: x * 2 if x % 2 == 0 else x ** 2
result = double_or_square(3)
print(result)

a) Doubles the number if even, squares it if odd

b) Doubles the number if odd, squares it if even

c) Squares the number if even, doubles it if odd

d) Doubles the number if positive, squares it if negative

Answer: A

Explanation: The lambda function double_or_square doubles the number if it’s even and squares it if it’s odd.

Q22. What is the output of the following code?

students = ['Alice', 'Bob', 'Charlie', 'David']
sorted_students = sorted(students, key=lambda x: len(x))
print(sorted_students)

a) [‘Bob’, ‘Alice’, ‘David’, ‘Charlie’]

b) [‘Alice’, ‘Bob’, ‘Charlie’, ‘David’]

c) [‘David’, ‘Alice’, ‘Bob’, ‘Charlie’]

d) [‘Alice’, ‘Charlie’, ‘Bob’, ‘David’]

Answer: a

Explanation: The sorted() function sorts the list students based on the length of each name, resulting in [‘Bob’, ‘Alice’, ‘David’, ‘Charlie’].

Q23. What is the purpose of the lambda keyword in Python?

a) To define a regular function

b) To define a class method

c) To create anonymous functions

d) To create generator functions

Answer: c

Explanation: The lambda keyword is used to create anonymous functions, which are functions without a name.

Q24. Which of the following statements is true about lambda functions?

a) Lambda functions can contain multiple lines of code

b) Lambda functions can have docstrings

c) Lambda functions can be decorated

d) Lambda functions can use the yield keyword

Answer: C

Explanation: Lambda functions can be decorated. Other options are false.

Q25. What will the following code snippet output?

full_name = lambda first, last: f"{first.capitalize()} {last.capitalize()}"
result = full_name('john', 'doe')
print(result)

a) “John Doe”

b) “john doe”

c) “John doe”

d) “john Doe”

Answer: a

Explanation: The lambda function full_name capitalizes the first and last names and returns them as a full name.

Q26. Which of the following functions is equivalent to the lambda function lambda x: x ** 3?

a) def cube(x): return x * x * x

b) def cube(x): return x ** 2

c) def cube(x): return x * 3

d) def cube(x): return x + 3

Answer: a

Explanation: The lambda function lambda x: x ** 3 calculates the cube of x, which is equivalent to def cube(x): return x * x * x.

Q27. What will be the output of the following code?

operations = {
    'add': lambda x, y: x + y,
    'subtract': lambda x, y: x - y,
    'multiply': lambda x, y: x * y
}

result_add = operations['add'](5, 3)
result_subtract = operations['subtract'](10, 4)
result_multiply = operations['multiply'](2, 6)

print(result_add, result_subtract, result_multiply)

a) 8 6 12

b) 3 6 12

c) 8 6 36

d) 3 6 36

Answer: a

Explanation: The dictionary operations contains lambda functions for addition, subtraction, and multiplication. The code then calls these functions with different arguments.

Q28. Which of the following is a correct lambda function to check if a number is prime?

a) lambda x: True if x % 2 == 0 else False

b) lambda x: True if x % 2 != 0 else False

c) lambda x: all(x % i != 0 for i in range(2, x))

d) lambda x: any(x % i == 0 for i in range(2, x))

Answer: c

Explanation: The lambda function lambda x: all(x % i != 0 for i in range(2, x)) checks if a number is prime by testing all numbers from 2 to x-1.

Q29. What is the output of the following code?

func = lambda x: x[1:]
result = func("Python")
print(result)

a) “ython”

b) “Python”

c) “P”

d) “y”

Answer: a

Explanation: The lambda function func returns the string x without its first character.

Q30. Which of the following is a correct lambda function to convert a string to uppercase?

a) lambda s: s.upper()

b) lambda s: s.capitalize()

c) lambda s: s.lower()

d) lambda s: s.title()

Answer: a

Explanation: The lambda function lambda s: s.upper() converts a string s to uppercase.

Q31. What does the following lambda function do?

add_two = lambda x, y: x + y
result = add_two(3, 4)
print(result)

a) Adds the two numbers

b) Multiplies the two numbers

c) Subtracts the second number from the first

d) Raises the first number to the power of the second

Answer: a

Explanation: The lambda function add_two adds its two arguments x and y, so add_two(3, 4) results in 3 + 4 = 7.

Q32. What will be the output of the following code?

names = ["Alice", "Bob", "Charlie", "David"]
sorted_names = sorted(names, key=lambda x: x[-1])
print(sorted_names)

a) [“Charlie”, “David”, “Alice”, “Bob”]

b) [“Alice”, “David”, “Bob”, “Charlie”]

c) [“Bob”, “Alice”, “David”, “Charlie”]

d) [“Alice”, “Charlie”, “Bob”, “David”]

Answer: C

Explanation: The sorted() function sorts the list names based on the last character of each name, resulting in [“Bob”, “David”, “Alice”, “Charlie”].

Congratulations on completing the Python Lambda Function MCQs! Lambda functions provide a concise and powerful way to create small functions in Python without the need for a full function definition. By mastering lambda functions, you gain the ability to write more expressive and efficient code for tasks that require short, simple functions. Keep practicing and experimenting with lambda functions to become proficient in using this versatile tool. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

Abhishek Patel
Abhishek Patel

it is too nice blog to revise concept.

narasimman P
narasimman P

In some MCQs answer were incorrect . please correct it

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details