What is the output of the following?
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
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. Since incr(x) returns x+1, each element is incremented by 1: 0→1, 1→2, 2→3. The list() wrapper converts the map object to a list, producing [1, 2, 3].
Step-by-step Derivation:
Step-by-step execution:
- Define elements = [0, 1, 2]
- Define incr function that returns x+1
- map(incr, elements) applies incr to each element:
- incr(0) = 0+1 = 1
- incr(1) = 1+1 = 2
- incr(2) = 2+1 = 3
- list() converts the map object to [1, 2, 3]
- print() outputs: [1, 2, 3]