ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
from contextlib import contextmanager
@contextmanager
def cm():
    print("A", end=" ")
    try:
        yield "B"
    except ZeroDivisionError:
        print("C", end=" ")
    print("D", end=" ")
with cm() as v:
    print(v, end=" ")
    1 / 0
print("E")
  1. A. A B C E
  2. B. A B D E
  3. C. A B C D ZeroDivisionError
  4. D. A B C D E

Correct answer: D. A B C D E

The exception raised in the with block is thrown into the generator at the yield; it is caught there, the generator runs to completion (printing D), and since it did not re-raise, the exception is suppressed.

More Python Programming questions

Exams that ask this topic