OA. free
Free
MathWorks Data Structures & Algorithms Data Structures & Algorithms Medium

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

Choose 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:

  1. Define elements = [0, 1, 2]
  2. Define incr function that returns x+1
  3. map(incr, elements) applies incr to each element:
    • incr(0) = 0+1 = 1
    • incr(1) = 1+1 = 2
    • incr(2) = 2+1 = 3
  4. list() converts the map object to [1, 2, 3]
  5. print() outputs: [1, 2, 3]