ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
g = (x for x in range(3))
print(sum(g), sum(g))
  1. A. 3 3
  2. B. 3 0
  3. C. 0 3
  4. D. 0 0

Correct answer: B. 3 0

sum(g) exhausts the generator, consuming 0+1+2=3. The second sum(g) gets an already-exhausted generator, which yields nothing, so it returns 0.

More Python Programming questions

Exams that ask this topic