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")- A. A B C E
- B. A B D E
- C. A B C D ZeroDivisionError
- 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
- What is printed? python from itertools import accumulate, chain, islice, count print(list(accumulate([1, 2, 3, 4])), list(chain("ab"…
- What is printed? python from itertools import ziplongest, product, combinations, permutations print(list(ziplongest("ab", [1, 2, 3]…
- What is printed? (Output lines are shown separated by " / ".) python from itertools import groupby data = [1, 1, 2, 1, 3, 3] print([(k…
- What is printed? python from functools import reduce print(reduce(lambda a, b: a 10 + b, [1, 2, 3]), reduce(lambda a, b: a + b, [], 7)…
- What is printed? python from functools import lrucache calls = 0 @lrucache(maxsize=2) def f(x): global calls calls += 1 return x for v in…
- What is printed? (Output lines are shown separated by " / ".) python from functools import partial def power(base, exp): return base exp…