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"))- A. done
- B. F 1
- C. F done
- 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
- 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)
- What is printed? python total = 0 squares = [total := total + v for v in [1, 2, 3]] print(squares, total)