ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
gen = (i for i in range(3))
print(1 in gen, 1 in gen, list(gen))
  1. A. True False []
  2. B. True True [0, 1, 2]
  3. C. True False [2]
  4. D. True True [2]

Correct answer: A. True False []

`in` on an iterator consumes elements until it finds a match. The first test consumes 0 and 1; the second test consumes 2 looking for another 1 and fails, so list() gets an exhausted generator.

More Python Programming questions

Exams that ask this topic