ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def g():
    try:
        yield 1
    except GeneratorExit:
        print("exit", end=" ")
        yield 2
it = g()
next(it)
try:
    it.close()
except RuntimeError:
    print("RuntimeError")
  1. A. exit
  2. B. exit RuntimeError
  3. C. RuntimeError
  4. D. exit 2

Correct answer: B. exit RuntimeError

close() throws GeneratorExit into the generator. If the generator responds by yielding another value instead of finishing, close() raises RuntimeError ('generator ignored GeneratorExit').

More Python Programming questions

Exams that ask this topic