ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
data = "2\n10 20\n30\n"
it = iter(data.split())
n = int(next(it))
arr = [int(next(it)) for _ in range(n)]
print(arr, next(it, 'none'))
  1. A. [10, 20] 30
  2. B. [10, 20] 2
  3. C. [2, 10] 30
  4. D. [10, 20] none

Correct answer: A. [10, 20] 30

The token iterator reads n = 2 first, then the next two tokens 10 and 20. The third token is still unread, so next(it, 'none') returns '30'. Token iterators are the standard way to read mixed-length input.

More Python Programming questions

Exams that ask this topic