OA. free
Free
MathWorks Core Computer Science Core Computer Science Medium

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

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

  1. elements = [0, 1, 2]
  2. incr(0) = 0 + 1 = 1
  3. incr(1) = 1 + 1 = 2
  4. incr(2) = 2 + 1 = 3
  5. map(incr, elements) creates a map object
  6. list(map object) converts to [1, 2, 3]
  7. print() outputs: [1, 2, 3]