ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
out = []
for x in [1, 0, "a"]:
try:
r = 10 / x
except (ZeroDivisionError, TypeError) as e:
out.append(type(e).__name__[0])
else:
out.append("ok")
finally:
out.append("|")
print("".join(out))- A. ok|Z|T|
- B. ok|Z|ok|T|ok|
- C. ok|
- D. okZT|
Correct answer: A. ok|Z|T|
else runs only when the try body raised nothing; finally runs every time. 10/1 gives ok, 10/0 raises ZeroDivisionError and 10/'a' raises TypeError.
More Python Programming questions
- What is printed? python class AppError(Exception): pass class DBError(AppError): pass try: raise DBError("db") except Exception…
- What is printed? python def f(): try: raise ValueError("a") except ValueError as e: err = e return err x = f() print(type(x).name, x.args…
- What is printed? python try: raise ExceptionGroup("g", [ValueError(1), TypeError(2), ValueError(3)]) except ValueError as eg…
- What is printed? python def f(n): try: if n: raise IndexError return "body" except IndexError: return "except" else: return "else"…
- What is printed? python import math x = float("inf") print(x - x, x 0 == x 0, math.isnan(x / x), -x < 0 < x, 1e308 10 == x)
- What is printed? python print(0.1 3, round(0.1 3, 2), 1.1 + 2.2 == 3.3, round(1.1 + 2.2, 1) == 3.3)