ProgrammingHardGATE CS · Product-company placement

What is printed?

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

Correct answer: C. F done

close() raises GeneratorExit at the paused yield, so the finally clause prints F. The generator is then finished, so next(it, 'done') returns the default.

More Python Programming questions

Exams that ask this topic