ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def evens():
    n = 0
    while True:
        yield n
        n += 2
def take(k, it):
    return [next(it) for _ in range(k)]
e = evens()
print(take(3, e), take(2, e), take(1, evens()))
  1. A. [0, 2, 4] [0, 2] [0]
  2. B. [0, 2, 4] [6, 8] [10]
  3. C. [0, 2, 4] [6, 8] [0]
  4. D. [0, 2, 4] [4, 6] [0]

Correct answer: C. [0, 2, 4] [6, 8] [0]

The same generator object e keeps its state between take calls, so the second call continues from 6. A new evens() starts again at 0.

More Python Programming questions

Exams that ask this topic