ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def f():
l = []
for i in range(3):
try:
if i == 0:
raise ValueError
l.append(i)
except ValueError:
l.append("v")
continue
finally:
l.append("f")
l.append("e")
return l
print(f())- A. ['v', 'f', 1, 'f', 'e', 2, 'f', 'e']
- B. ['v', 1, 'f', 'e', 2, 'f', 'e']
- C. ['v', 'f', 'e', 1, 'f', 'e', 2, 'f', 'e']
- D. ['v', 'f', 1, 'e', 'f', 2, 'e', 'f']
Correct answer: A. ['v', 'f', 1, 'f', 'e', 2, 'f', 'e']
finally runs even when continue leaves the try statement, so i = 0 gives 'v', 'f' and skips 'e'. For i = 1 and 2 the try succeeds, finally runs, and then the statement after the try appends 'e'.
More Python Programming questions
- What is printed? python def risky(n): if n == 1: raise KeyError(n) if n == 2: raise IndexError(n) return n res = [] for n in range(4)…
- What is printed? python class E(Exception): def str(self): return "custom" try: try: raise E("orig") except E as e: raise…
- What is printed? python def f(x): try: return int(x) except ValueError: return float(x) except TypeError: return -1 print(f("3")…
- What is printed? python import math print(math.isclose(0.1 + 0.2, 0.3), math.isclose(1e-10, 0), math.isclose(1e-10, 0, abstol=1e-9)…
- What is printed? python a = 0.1 + 0.2 print(f"{a:.20f}"[:6], a == 0.3, round(a, 15) == round(0.3, 15), str(a) == "0.30000000000000004"…
- What is printed? python print(10 / 4, 10 // 4, -10 // 4, 10 % -4, 2 -2, 7 // 2.0)