ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def gen():
    try:
        x = yield 1
    except GeneratorExit:
        print("closing")
        raise
g = gen()
next(g)
g.close()
  1. A. Nothing is printed
  2. B. closing
  3. C. 1 closing
  4. D. closing 1

Correct answer: B. closing

next(g) advances to the yield, suspending there. g.close() throws GeneratorExit into the generator at that exact point, which is caught, prints 'closing', and then re-raised (as required) to properly finish closing the generator.

More Python Programming questions

Exams that ask this topic