ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def countdown(n):
    while n > 0:
        yield n
        n -= 1
g = countdown(3)
print(list(g))
print(list(g))
  1. A. [3, 2, 1] [3, 2, 1]
  2. B. [3, 2, 1] []
  3. C. [1, 2, 3] []
  4. D. [] [3, 2, 1]

Correct answer: B. [3, 2, 1] []

The first list(g) drives the generator to exhaustion, collecting 3, 2, 1. A generator cannot be restarted: the second list(g) finds it already finished and returns an empty list.

More Python Programming questions

Exams that ask this topic