ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
def gen():
    yield 1
    return 5
    yield 2

print(list(gen()))
  1. A. [1]
  2. B. [1, 5, 2]
  3. C. [1, 5]
  4. D. [1, 2]

Correct answer: A. [1]

return inside a generator ends the iteration (its value goes into StopIteration.value and list() ignores it). The code after return never runs, so only 1 is produced.

More Python Programming questions

Exams that ask this topic