ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def g():
yield 1
raise StopIteration
try:
print(list(g()))
except RuntimeError as e:
print("RuntimeError")- A. [1]
- B. []
- C. RuntimeError
- D. [1, None]
Correct answer: C. RuntimeError
Since PEP 479 (Python 3.7+), a StopIteration raised inside a generator body is converted to RuntimeError, so list() does not silently stop; the RuntimeError propagates.
More Python Programming questions
- What is printed? python def g(): try: yield 1 finally: print("F", end=" ") it = g() next(it) it.close() print(next(it, "done"))
- What is printed? python import itertools a, b = itertools.tee(iter([1, 2, 3])) next(a); next(a) print(list(b), list(a))
- What is printed? python def nums(): for n in range(5): print(n, end="") yield n r = any(n > 1 for n in nums()) print("", r)
- What is printed? python gen = (i for i in range(3)) print(1 in gen, 1 in gen, list(gen))
- What is printed? python x = 10 class K: x = 20 y = [x + i for i in range(2)] z = (lambda: x)() print(K.y, K.z)
- What is printed? python class K: n = 3 a = [i for i in range(n)] try: b = [n for i in range(2)] except NameError: b = "NE" print(K.a, K.b)