ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
it = (x * x for x in range(5))
first = next(it)
rest = [v for v in it if v % 2 == 0]
again = sum(1 for _ in it)
print(first, rest, again)
  1. A. 0 [0, 4, 16] 0
  2. B. 0 [4, 16] 5
  3. C. 0 [1, 9] 0
  4. D. 0 [4, 16] 0

Correct answer: D. 0 [4, 16] 0

next takes the first square 0; the comprehension consumes the rest (1, 4, 9, 16), keeping the even ones. The generator is then exhausted, so the final count is 0.

More Python Programming questions

Exams that ask this topic