ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def g():
    yield 1
    yield 2
it = g()
for v in it:
    print(v, end=" ")
    break
for v in it:
    print(v, end=" ")
print(list(it))
  1. A. 1 2 []
  2. B. 1 1 2 []
  3. C. 1 2 [1, 2]
  4. D. 1 []

Correct answer: A. 1 2 []

break leaves the generator suspended after yielding 1; the second loop resumes it and gets 2, exhausting it. list() on an exhausted generator is [].

More Python Programming questions

Exams that ask this topic