ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
calls = []
def f(x):
    calls.append(x)
    return x * 2
m = map(f, [1, 2, 3])
print(calls)
first = next(m)
print(calls, first)
print(list(m), calls)
  1. A. [1, 2, 3] | [1] 2 | [4, 6] [1, 2, 3]
  2. B. [] | [1, 2] 2 | [4, 6] [1, 2]
  3. C. [] | [1] 2 | [4, 6] [1]
  4. D. [] | [1] 2 | [4, 6] [1, 2, 3]

Correct answer: D. [] | [1] 2 | [4, 6] [1, 2, 3]

map is lazy, so nothing runs until next is called. After one next only 1 has been processed, and list(m) consumes the rest. The calls list is one object, so the last print shows all three calls.

More Python Programming questions

Exams that ask this topic