Welcome to the Python Lambda Operate MCQs! Lambda capabilities, often known as nameless capabilities, are concise and highly effective instruments in Python for creating small, inline capabilities. They’re typically utilized in eventualities the place a small perform is required for a brief time frame and a full perform definition could be cumbersome. These questions will take a look at your understanding of lambda capabilities in Python, together with their syntax, utilization, advantages, and limitations. Every query is multiple-choice, with just one appropriate reply. Take your time to rigorously learn every query and select the best choice. Let’s discover the world of Python lambda capabilities collectively!

## 30+ MCQs on Python Lambda Operate

#### Q1. What’s a lambda perform in Python?

a) A built-in perform for performing mathematical calculations

b) A perform that’s outlined with the `def`

key phrase

c) An nameless perform outlined utilizing the `lambda`

key phrase

d) A perform that may solely be used with strings

**Reply:** c

**Rationalization: **Lambda capabilities in Python are nameless capabilities outlined with the `lambda`

key phrase.

#### Q2. Which of the next statements is true about lambda capabilities?

a) Lambda capabilities can have a number of expressions

b) Lambda capabilities can have default arguments

c) Lambda capabilities can’t have return statements

d) Lambda capabilities will be named and reused like common capabilities

**Reply:** b

**Rationalization:** Lambda capabilities can have default arguments, permitting flexibility of their utilization.

#### Q3. What’s the syntax for a lambda perform in Python?

a) `lambda arg1, arg2: expression`

b) `def lambda(arg1, arg2): expression`

c) `lambda perform(arg1, arg2): expression`

d) `perform(arg1, arg2): expression`

**Reply:** a

**Rationalization:** The syntax for a lambda perform is `lambda arguments: expression`

.

#### This autumn. When are lambda capabilities sometimes utilized in Python?

a) To outline massive, advanced capabilities

b) To create nameless capabilities for easy duties

c) To interchange built-in capabilities like `print()`

and `enter()`

d) To deal with exceptions in code

**Reply:** b

**Rationalization:** Lambda capabilities are generally used for creating small, nameless capabilities for easy duties.

#### Q5. Which of the next is a sound use case for a lambda perform?

a) Sorting a listing of tuples by the second factor

b) Defining a category technique

c) Dealing with file I/O operations

d) Performing matrix multiplication

**Reply:** a

**Rationalization:** Lambda capabilities are sometimes used for sorting, particularly when the sorting secret’s primarily based on a particular factor in a tuple or object.

#### Q6. What’s the output of the next code?

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

a) 5

b) 10

c) 25

d) 15

**Reply:** b

**Rationalization:** The lambda perform `(lambda x: x * 2)`

multiplies the enter `5`

by `2`

, leading to `10`

.

#### Q7. Which of the next is true about lambda capabilities?

a) They can not have a number of parameters

b) They’re at all times named

c) They can be utilized to create recursive capabilities

d) They will include a number of traces of code

**Reply: **a

**Rationalization:** Lambda capabilities are restricted to a single expression, so that they sometimes have a single parameter.

#### Q8. What’s the objective of utilizing lambda capabilities with `map()`

and `filter()`

capabilities in Python?

a) To use the lambda perform to all components of an iterable

b) To take away all components from an iterable

c) To kind the weather of an iterable

d) To create a brand new iterable with chosen components

**Reply:** a

**Rationalization:** Lambda capabilities are sometimes used with `map()`

and `filter()`

to use a perform to all components of an iterable and filter components primarily based on a situation, respectively.

#### Q9. What would be the output of the next code?

```
nums = [1, 2, 3, 4, 5]
end result = checklist(map(lambda x: x * x, nums))
print(end 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]

**Reply:** b

**Rationalization:** The `map()`

perform applies the lambda perform to every factor in `nums`

, leading to a brand new checklist with squared values.

#### Q10. What’s the output of the next code?

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

a) [1, 3, 5]

b) [2, 4]

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

d) []

**Reply:** b

**Rationalization:** The `filter()`

perform creates a brand new checklist with components that fulfill the situation (even numbers) given by the lambda perform.

#### Q11. What’s going to the next code snippet output?

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

a) 2

b) 3

c) 5

d) 6

**Reply:** c

**Rationalization:** The lambda perform `f`

provides its two arguments `x`

and `y`

, so `f(2, 3)`

ends in `2 + 3 = 5`

.

#### Q12. Which of the next is an accurate lambda perform to calculate the sq. of a quantity?

a) `lambda x: x * x`

b) `lambda x: x ^ 2`

c) `lambda x: x ** 2`

d) `lambda x: x + x`

**Reply:** c

**Rationalization:** The lambda perform `lambda x: x ** 2`

calculates the sq. of a quantity `x`

.

#### Q13. What does the next lambda perform do?

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

a) Returns the quantity whether it is even, in any other case None

b) Returns None if the quantity is even, in any other case the quantity

c) Returns the sq. of the quantity whether it is even, in any other case None

d) Returns the quantity whether it is odd, in any other case None

**Reply:** b

**Rationalization:** The lambda perform `lambda x: x if x % 2 == 0 else None`

returns None if the enter quantity `x`

is even, in any other case it returns the quantity itself.

#### Q14. What would be the output of the next code?

```
greet = lambda: "Hiya, World!"
end result = greet()
print(end result)
```

a) “Hiya, World!”

b) “Hiya!”

c) None

d) Error: lambda capabilities require arguments

**Reply:** a

**Rationalization:** The lambda perform `greet`

has no arguments and returns the string “Hiya, World!”, which is then printed.

#### Q15. What’s the output of the next code?

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

a) [7, 10, 25]

b) [10, 25, 7]

c) [7, 10, 5]

d) [10, 5, 25]

**Reply:** a

**Rationalization:** Every lambda perform in `func_list`

is utilized to the worth `5`

, leading to `[5 + 2, 5 * 2, 5 ** 2]`

.

#### Q16. Which of the next is an accurate use of a lambda perform with a number of arguments?

a) `lambda x, y: x * y`

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

c) `lambda x y: x * y`

d) `lambda x: x * y`

**Reply:** a

**Rationalization:** The right syntax for a lambda perform with a number of arguments is `lambda x, y: x * y`

.

#### Q17. What does the next lambda perform do?

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

a) Returns the sq. of a constructive quantity, in any other case None

b) Returns None if the quantity is constructive, in any other case the sq. of the quantity

c) Returns the sq. of the quantity whether it is constructive, in any other case None

d) Returns the quantity whether it is constructive, in any other case its sq.

**Reply:** c

**Rationalization:** The lambda perform `square_if_positive`

returns the sq. of the quantity whether it is constructive, in any other case it returns None.

#### Q18. What’s the output of the next code?

```
nums = [1, 2, 3, 4, 5]
end result = checklist(map(lambda x: x % 2 == 0, nums))
print(end 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]

**Reply:** a

**Rationalization:** The `map()`

perform applies the lambda perform to every factor in `nums`

, leading to a listing of Boolean values indicating whether or not every factor is even.

#### Q19. Which of the next is a sound lambda perform to examine if a quantity is damaging?

a) `lambda x: x < 0`

b) `lambda x: x > 0`

c) `lambda x: x == 0`

d) `lambda x: x != 0`

**Reply: **a

**Rationalization:** The lambda perform `lambda x: x < 0`

checks if a quantity is damaging.

#### Q20. What’s going to the next code snippet output?

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

a) “even”

b) “odd”

c) 7

d) None

**Reply: **b

**Rationalization:** The lambda perform `f`

checks if a quantity is even or odd and returns the respective string.

#### Q21. What does the next lambda perform do?

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

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

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

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

d) Doubles the quantity if constructive, squares it if damaging

**Reply:** b

**Rationalization:** The lambda perform `double_or_square`

doubles the quantity if it’s odd and squares it if it’s even.

#### Q22. What’s the output of the next code?

```
college students = ['Alice', 'Bob', 'Charlie', 'David']
sorted_students = sorted(college 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’]

**Reply:** a

**Rationalization:** The `sorted()`

perform types the checklist `college students`

primarily based on the size of every title, leading to [‘Bob’, ‘Alice’, ‘David’, ‘Charlie’].

#### Q23. What’s the objective of the `lambda`

key phrase in Python?

a) To outline an everyday perform

b) To outline a category technique

c) To create nameless capabilities

d) To create generator capabilities

**Reply: **c

**Rationalization:** The `lambda`

key phrase is used to create nameless capabilities, that are capabilities and not using a title.

#### Q24. Which of the next statements is true about lambda capabilities?

a) Lambda capabilities can include a number of traces of code

b) Lambda capabilities can have docstrings

c) Lambda capabilities will be embellished

d) Lambda capabilities can use the `yield`

key phrase

**Reply: **b

**Rationalization:** Lambda capabilities can’t have docstrings, as they’re restricted to a single expression.

#### Q25. What’s going to the next code snippet output?

```
full_name = lambda first, final: f"first.capitalize() final.capitalize()"
end result = full_name('john', 'doe')
print(end result)
```

a) “John Doe”

b) “john doe”

c) “John doe”

d) “john Doe”

**Reply: **a

**Rationalization: **The lambda perform `full_name`

capitalizes the primary and final names and returns them as a full title.

#### Q26. Which of the next capabilities is equal to the lambda perform `lambda x: x ** 3`

?

a) `def dice(x): return x * x * x`

b) `def dice(x): return x ** 2`

c) `def dice(x): return x * 3`

d) `def dice(x): return x + 3`

**Reply: **a

**Rationalization:** The lambda perform `lambda x: x ** 3`

calculates the dice of `x`

, which is equal to `def dice(x): return x * x * x`

.

#### Q27. What would be the output of the next 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

**Reply:** a

**Rationalization:** The dictionary `operations`

incorporates lambda capabilities for addition, subtraction, and multiplication. The code then calls these capabilities with completely different arguments.

#### Q28. Which of the next is an accurate lambda perform to examine if a quantity 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 vary(2, x))`

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

**Reply: **c

**Rationalization:** The lambda perform `lambda x: all(x % i != 0 for i in vary(2, x))`

checks if a quantity is prime by testing all numbers from 2 to `x-1`

.

#### Q29. What’s the output of the next code?

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

a) “ython”

b) “Python”

c) “P”

d) “y”

**Reply:** a

**Rationalization:** The lambda perform `func`

returns the string `x`

with out its first character.

#### Q30. Which of the next is an accurate lambda perform to transform a string to uppercase?

a) `lambda s: s.higher()`

b) `lambda s: s.capitalize()`

c) `lambda s: s.decrease()`

d) `lambda s: s.title()`

**Reply:** a

**Rationalization:** The lambda perform `lambda s: s.higher()`

converts a string `s`

to uppercase.

#### Q31. What does the next lambda perform do?

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

a) Provides the 2 numbers

b) Multiplies the 2 numbers

c) Subtracts the second quantity from the primary

d) Raises the primary quantity to the facility of the second

**Reply: **a

**Rationalization:** The lambda perform `add_two`

provides its two arguments `x`

and `y`

, so `add_two(3, 4)`

ends in `3 + 4 = 7`

.

#### Q32. What would be the output of the next 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”]

**Reply:** b

**Rationalization:** The `sorted()`

perform types the checklist `names`

primarily based on the final character of every title, leading to [“Alice”, “David”, “Bob”, “Charlie”].

Congratulations on finishing the Python Lambda Operate MCQs! Lambda capabilities present a concise and highly effective approach to create small capabilities in Python with out the necessity for a full perform definition. By mastering lambda capabilities, you acquire the flexibility to jot down extra expressive and environment friendly code for duties that require brief, easy capabilities. Maintain working towards and experimenting with lambda capabilities to develop into proficient in utilizing this versatile software. When you have any questions or wish to delve deeper into any matter, don’t hesitate to proceed your studying journey. Glad coding!

It’s also possible to enroll in our free Python Course At the moment!

Learn our extra articles associated to MCQs in Python: