ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
def f():
try:
return "try"
finally:
print("finally", end=" ")
def g():
try:
return "try"
finally:
return "finally"
print(f(), g())- A. try finally
- B. finally try try
- C. finally finally finally
- D. finally try finally
Correct answer: D. finally try finally
finally always runs: f prints 'finally' before its 'try' value is returned. In g the return inside finally overrides the pending return value, so g() gives 'finally'. f() is evaluated (and prints) before print outputs anything.
More Python Programming questions
- What is printed? python nums = [3, 1, 2] r = nums.sort() s = sorted(nums, reverse=True) print(r, nums, s)
- What is printed? python x = 10 def outer(): x = 20 def inner(): nonlocal x x += 1 return x return inner() + x print(outer(), x)
- What is printed? python d = {} d[1] = "a" d[1.0] = "b" d[True] = "c" print(d, len(d))
- What is printed? python a = [1, 2, 3, 4, 5, 6] for x in a: if x % 2 == 0: a.remove(x) print(a)
- What is printed? python print(round(2.5), round(3.5), round(-0.5), int(-2.7), -7 // 2, -7 % 2)
- What is printed? python from collections import Counter c = Counter("mississippi") print(c.mostcommon(2), c["z"], sum(c.values()))