ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
def f():
try:
return 1
finally:
return 2
print(f())- A. 1
- B. None
- C. SyntaxError
- D. 2
Correct answer: D. 2
finally runs even after a return in try, and a return inside finally replaces the pending return value, so 2 is returned. (Python 3.14 adds a SyntaxWarning for this pattern, but it still runs.)
More Python Programming questions
- What is printed? python print(1 < 2 > 1.5, 3 > 2 == 2, (3 > 2) == 2)
- What is printed? python a = [1, 2, 3] b = a a = a + [4] b += [5] print(a, b)
- What is printed? python print(-2 2, (-2) 2, 2 -1)
- What is printed? python print(0 or [] or "x", 1 and 0 and 5, None or 0)
- What is printed? python x = [1, 2] g = (n 10 for n in x) x.append(3) x = [7] print(list(g))
- What is printed? python class Bag: items = [] def add(self, v): self.items.append(v) a, b = Bag(), Bag() a.add(1); b.add(2) print(a.items…