43.
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
(Python Question) The power of map**
What is the output of the following?
elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(incr, elements)))
Pick ONE option
Show answer & explanation
Answer: A. [1, 2, 3]
The map() function applies the incr() function to each element in the elements list. For each element (0, 1, 2), it adds 1 and returns the result (1, 2, 3 respectively). The list() conversion transforms the map object into a list, yielding [1, 2, 3].
Step-by-step Derivation:
Execution trace:
- elements = [0, 1, 2]
- incr(0) = 0 + 1 = 1
- incr(1) = 1 + 1 = 2
- incr(2) = 2 + 1 = 3
- map(incr, elements) creates a map object
- list(map object) converts to [1, 2, 3]
- print() outputs: [1, 2, 3]