ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
def g():
try:
yield 1
yield 2
finally:
print("cleanup", end=" ")
it = g()
print(next(it), end=" ")
it.close()
print(list(it))- A. 1 cleanup []
- B. 1 [2]
- C. 1 cleanup [2]
- D. 1 [] cleanup
Correct answer: A. 1 cleanup []
close() raises GeneratorExit at the paused yield, so the finally block runs immediately and prints "cleanup". The generator is then finished; iterating it again yields nothing, so list(it) is []. Value 2 is never produced.
More Python Programming questions
- What is printed? python data = [4, 7, 1, 9] best = 0 hi = [best := x for x in data if x > best] print(hi, best, "x" in globals())
- What is printed? python class M: def init(self, v): self.v = v def add(self, o): return M(self.v + (o.v if isinstance(o, M) else o)) ms =…
- What is printed? python print(f"{3.14159:.2f}|{42:>5}|{'ab':^6}|{255:#x}|{1234567:,}")
- What is printed? python grid = [[0, 0]] 2 copy1 = list(grid) copy2 = [row[:] for row in grid] grid[0][0] = 1 grid[1] = [5, 5] print(copy1…
- What is printed? python vals = iter([3, 1, 0, 4, 0]) print(list(iter(lambda: next(vals), 0)), list(vals))
- What is printed? python class Acc: def init(self): self.items = [] def iadd(self, v): self.items.append(v) a = Acc() b = a a += 1 print(a…