ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
def h():
try:
x = 1 / 0
except ZeroDivisionError:
return "caught"
else:
return "no error"
finally:
print("always")
print(h())- A. caught always
- B. always caught
- C. no error always
- D. always no error
Correct answer: B. always caught
finally runs just before the function's already-decided return value is actually handed back, so 'always' prints first, and then h() returns 'caught' (from the except clause, since the else clause never runs after an exception).
More Python Programming questions
- What is printed? python def k(): try: return "try" except Exception: return "except" else: return "else" print(k())
- What is printed? python s = "placement" print(s[-100:5])
- What is printed? python s = "abcdef" print(s[10:20])
- What is printed? python s = "abcdef" s2 = list(s) s2[1:3] = ["X", "Y", "Z"] print("".join(s2))
- What is printed? python s = "abc" print(s 0, type(s 0))
- What is printed? python s = "Mississippi" print(s.count("ss"), s.count("i"))