ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def g():
try:
yield 1
yield 2
finally:
print('cleanup')
it = g()
print(next(it))
it.close()
print(list(it))- A. 1 | cleanup | []
- B. 1 | 2 | []
- C. 1 | cleanup | [1, 2]
- D. 1 | 2 | cleanup
Correct answer: A. 1 | cleanup | []
close() raises GeneratorExit at the paused yield, so the finally block runs and prints cleanup. A closed generator yields nothing more, so list gives [].
More Python Programming questions
- What is printed? python print('hello world'.translate({ord('l'): None, ord('o'): 'O'}))
- What is printed? python class N: def init(self, v): self.v = v self.nxt = None def hascycle(h): slow = fast = h steps = 0 while fast and…
- What is printed? python print(hash(-1), hash(-2), hash(-1) == -2, hash(1) == hash(1.0))
- What is printed? python print(232, (23)2, -22, (-2)2, 2-12)
- What is printed? python print(float(254) + 1 == 254, 254 + 1 == float(254), 1e16 + 2 == 1e16 + 1)
- What is printed? python print(int('١٢٣'), int('0' 2 + '5'), int('5' + '0' 2), int(' t7n'))