Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: What does this snippet print? gen = (x*x for x in range(4)) print(sum(gen), sum(gen))

  1. A. 14 14
  2. B. 0 0
  3. C. 0 14
  4. D. 14 0

Correct answer: D. 14 0

A generator can be consumed only once. The first sum exhausts it, so the second sum sees nothing and returns 0.

More Coding Interview Problems questions

Exams that ask this topic