Welcome to the Python Map, Filter, and Reduce Functions **Python Interview Questions**! Map, filter, and reduce are powerful built-in functions in Python for functional programming paradigms. These functions allow you to apply a function to each element of a sequence, filter elements based on a condition, and reduce a sequence of elements to a single value. These questions will test your understanding of how to use map, filter, and reduce functions in Python, including their syntax, purpose, and examples. 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 map, filter, and reduce functions together!

`map()`

function in Python?a) To filter elements of a sequence based on a given function

b) To apply a function to every item in an iterable and return a list of the results

c) To reduce an iterable into a single cumulative value

d) To return a subset of elements from an iterable based on a condition

**Answer: **b

**Explanation:** The `map()`

function in Python applies a given function to every item in an iterable and returns a list of the results.

`map()`

function in Python?a) `map(function, sequence)`

b) `map(sequence, function)`

c) `map(sequence)(function)`

d) `map(function)(sequence)`

**Answer: **a

**Explanation:** The correct syntax for the `map()`

function is `map(function, sequence)`

where `function`

is the function to be applied and `sequence`

is the iterable.

`filter()`

function in Python do?a) Applies a function to every item in an iterable and returns a single cumulative value

b) Filters out elements of an iterable based on a given function

c) Returns a subset of elements from an iterable based on a condition

d) Maps a function to every item in an iterable and returns a list of the results

**Answer: **c

**Explanation:** The `filter()`

function in Python returns a subset of elements from an iterable based on a condition specified by a function.

`filter()`

function in Python?a) `filter(function, sequence)`

b) `filter(sequence, function)`

c) `filter(sequence)(function)`

d) `filter(function)(sequence)`

**Answer:** a

**Explanation:** The correct syntax for the `filter()`

function is `filter(function, sequence)`

where `function`

is the filtering function and `sequence`

is the iterable.

`reduce()`

function in Python do?a) Reduces an iterable into a single cumulative value using a function

b) Applies a function to every item in an iterable and returns a list of the results

c) Filters out elements of an iterable based on a given function

d) Maps a function to every item in an iterable and returns a list of the results

**Answer: **a

**Explanation: **The `reduce()`

function in Python reduces an iterable into a single cumulative value by applying a function repeatedly to pairs of items.

`reduce()`

function in Python?a) math

b) functools

c) itertools

d) operator

**Answer: **b

**Explanation:** The `reduce()`

function in Python is part of the `functools`

module, so `import functools`

is required to use it.

```
from functools import reduce
def multiply(x, y):
return x * y
numbers = [1, 2, 3, 4, 5]
result = reduce(multiply, numbers)
print(result)
```

a) 15

b) 120

c) 30

d) 10

**Answer: **b

**Explanation: **The code will output `120`

because `reduce()`

applies the `multiply`

function cumulatively to the items of the `numbers`

list.

```
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
```

a) Creates a list of squares of each number in `numbers`

b) Filters out even numbers from `numbers`

c) Reduces `numbers`

into a single cumulative value

d) Raises a syntax error

**Answer: **a

**Explanation: **The code creates a map object `squared`

containing the squares of each number in the `numbers`

list.

`lambda`

function in Python is true?a) The `lambda`

function can contain multiple expressions.

b) The `lambda`

function can have a return statement.

c) The `lambda`

function can have default arguments.

d) The `lambda`

function can only have a single expression.

**Answer: **d

**Explanation: **The `lambda`

function in Python can only have a single expression.

```
def even_check(num):
if num % 2 == 0:
return True
else:
return False
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(even_check, numbers)
print(list(even_numbers))
```

a) [1, 3, 5, 7, 9]

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

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

d) [2, 4, 6, 8]

**Answer: **b

**Explanation: **The code filters out even numbers from the `numbers`

list using the `even_check`

function and prints the resulting list.

`map()`

function?a) List comprehension

b) Generator expression

c) Iterator

d) Filter function

**Answer:** a

**Explanation: **List comprehension in Python is equivalent to the `map()`

function as it can be used to perform similar tasks of applying a function to every item in an iterable.

```
def add(a, b):
return a + b
numbers1 = [1, 2, 3, 4]
numbers2 = [5, 6, 7, 8]
sums = map(add, numbers1, numbers2)
print(list(sums))
```

a) [6, 8, 10, 12]

b) [1, 2, 3, 4, 5, 6, 7, 8]

c) [15, 18, 21, 24]

d) [1, 5, 2, 6, 3, 7, 4, 8]

**Answer:** a

**Explanation:** The code creates a `map`

object `sums`

that adds corresponding elements from `numbers1`

and `numbers2`

, resulting in `[6, 8, 10, 12]`

.

`operator`

module provide in Python?a) Functions corresponding to built-in operations

b) Functions for creating iterators

c) Functions for sorting lists

d) Functions for mathematical operations

**Answer: **a

**Explanation: **The `operator`

module in Python provides functions that correspond to built-in operations, making it useful for `map()`

and `reduce()`

functions.

```
def is_positive(num):
return num > 0
numbers = [-1, 3, -5, 7, -9]
positive_nums = filter(is_positive, numbers)
```

a) Create a list of positive numbers from `numbers`

b) Create a list of negative numbers from `numbers`

c) Create a list of even numbers from `numbers`

d) Create a list of all numbers from `numbers`

**Answer: **a

**Explanation: **The code filters out positive numbers from the `numbers`

list using the `is_positive`

function.

a) `apply()`

b) `join()`

c) `reduce()`

d) `combine()`

**Answer: **c

**Explanation: **The `reduce()`

function in Python is used to combine elements of an iterable using a specified function and reduce it to a single value.

```
from functools import reduce
numbers = [10, 20, 30, 40, 50]
total = reduce(lambda x, y: x + y, numbers)
print(total)
```

a) 150

b) 100

c) 200

d) 250

**Answer: **a

**Explanation: **The code will output `150`

because `reduce()`

adds all the numbers in the `numbers`

list together.

`map()`

function in Python is true?a) `map()`

always returns a list.

b) `map()`

can only be used with functions that take a single argument.

c) `map()`

can be used with multiple iterables.

d) `map()`

modifies the original iterable.

**Answer: **c

**Explanation: **The `map()`

function in Python can be used with multiple iterables, applying the given function to corresponding items.

```
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
```

a) Create a list of squares of each number in `numbers`

b) Create a list of cubes of each number in `numbers`

c) Create a list of even numbers from `numbers`

d) Create a list of all numbers from `numbers`

**Answer:** a

**Explanation:** The code creates a map object `squared`

containing the squares of each number in the `numbers`

list.

a) `reduce()`

b) `filter()`

c) `map()`

d) `apply()`

**Answer: **b

**Explanation:** The `filter()`

function in Python is used to filter a list of elements based on a given condition.

```
def is_even(num):
return num % 2 == 0
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = filter(is_even, numbers)
print(list(even_numbers))
```

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

b) [1, 3, 5, 7, 9]

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

d) [2, 4, 6, 8]

**Answer: **a

**Explanation: **The code filters out even numbers from the `numbers`

list using the `is_even`

function and prints the resulting list.

a) `reduce()`

b) `map()`

c) `filter()`

d) `apply()`

**Answer:** b

**Explanation:** The `map()`

function in Python is used to apply a given function to every item of an iterable.

```
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)
```

a) 120

b) 30

c) 15

d) 10

**Answer: **a

**Explanation: **The code will output `120`

because `reduce()`

multiplies all the numbers in the `numbers`

list together.

`reduce()`

function in Python?a) `reduce(sequence, function)`

b) `reduce(function, sequence)`

c) `reduce(sequence)(function)`

d) `reduce(function)(sequence)`

**Answer:** b

**Explanation:** The correct syntax for the `reduce()`

function is `reduce(function, sequence)`

where `function`

is the reducing function and `sequence`

is the iterable.

```
numbers = [10, 20, 30, 40, 50]
doubled = map(lambda x: x * 2, numbers)
```

a) Create a list of doubled numbers from `numbers`

b) Create a list of halved numbers from `numbers`

c) Create a list of even numbers from `numbers`

d) Create a list of all numbers from `numbers`

**Answer: **a

**Explanation: **The code creates a map object `doubled`

containing the doubled values of each number in the `numbers`

list.

a) `apply()`

b) `map()`

c) `filter()`

d) `reduce()`

**Answer:** b

**Explanation: **The `map()`

function in Python applies a function to each element of an iterable and returns a list of results.

```
def is_vowel(char):
vowels = 'aeiouAEIOU'
return char in vowels
chars = ['a', 'b', 'c', 'd', 'e', 'F', 'G', 'H', 'I', 'j']
filtered_chars = filter(is_vowel, chars)
print(list(filtered_chars))
```

a) [‘a’, ‘e’, ‘F’, ‘I’]

b) [‘a’, ‘e’, ‘I’]

c) [‘a’, ‘e’, ‘A’, ‘E’, ‘I’]

d) [‘a’, ‘e’]

**Answer: **b

**Explanation: **The code filters out the vowels from the `chars`

list using the `is_vowel`

function and prints the resulting list.

`operator`

module provide in Python?a) Functions corresponding to built-in operations

b) Functions for creating iterators

c) Functions for sorting lists

d) Functions for mathematical operations

**Answer:** a

**Explanation: **The `operator`

module in Python provides functions that correspond to built-in operations, making it useful for `map()`

and `reduce()`

functions.

```
import functools
numbers = [1, 2, 3, 4, 5]
sums = functools.reduce(lambda x, y: x + y, numbers)
print(sums)
```

a) 15

b) 10

c) 20

d) 25

**Answer:** a

**Explanation: **The code will output `15`

because `reduce()`

adds all the numbers in the `numbers`

list together.

```
def cube(num):
return num ** 3
numbers = [1, 2, 3, 4, 5]
cubed = map(cube, numbers)
print(list(cubed))
```

a) [1, 8, 27, 64, 125]

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

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

d) [3, 6, 9, 12, 15]

**Answer:** a

**Explanation: **The code will output `[1, 8, 27, 64, 125]`

because `map()`

applies the `cube`

function to each element in `numbers`

.

```
def square(num):
return num * num
numbers = [1, 2, 3, 4, 5]
squared = map(square, numbers)
print(list(squared))
```

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

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

c) [1, 3, 5, 7, 9]

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

**Answer: **a

**Explanation: **The code will output `[1, 4, 9, 16, 25]`

because `map()`

applies the `square`

function to each element in `numbers`

.

```
from functools import reduce
def multiply(x, y):
return x * y
numbers = [1, 2, 3, 4, 5]
product = reduce(multiply, numbers, 10)
print(product)
```

a) 150

b) 120

c) 100

d) 200

**Answer: **b

**Explanation: **The code will output `120`

because `reduce()`

multiplies all the numbers in the `numbers`

list together starting with an initial value of 10.

Congratulations on completing the Python Map, Filter, and Reduce Functions MCQs! Map, filter, and reduce functions are powerful tools for functional programming in Python, allowing you to apply transformations and operations to sequences of elements. By mastering these functions, you gain the ability to write concise and efficient code for data manipulation tasks. Keep practicing and experimenting with map, filter, and reduce functions to become proficient in using them effectively. 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:

- 30+ MCQs on Python Lambda Functions
- 30+ MCQs on Python Exception Handling
- 30+ MCQs on Python Regular Expression

Lorem ipsum dolor sit amet, consectetur adipiscing elit,