ProgrammingVery hardProduct company OA

What is printed?

What is printed?
vals = iter([3, 1, 0, 4, 0])
print(list(iter(lambda: next(vals), 0)), list(vals))
  1. A. [3, 1, 0, 4] [0]
  2. B. [3, 1] [4, 0]
  3. C. [3, 1] [0, 4, 0]
  4. D. [3, 1, 4] []

Correct answer: B. [3, 1] [4, 0]

iter(callable, sentinel) calls the function repeatedly and stops as soon as it returns the sentinel, without including it. The lambda pulls 3, 1, then 0, which stops the outer list at [3, 1]. That 0 was already consumed from vals, so the remaining items are [4, 0] — the second 0 is just data.

More Python Programming questions

Exams that ask this topic