ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class AppError(Exception): pass
class DBError(AppError): pass
try:
raise DBError("db")
except Exception:
print("Exception", end=" ")
except AppError:
print("AppError", end=" ")
print(issubclass(DBError, Exception), isinstance(DBError(), AppError))- A. AppError True True
- B. Exception True True
- C. Exception AppError True True
- D. AppError False True
Correct answer: B. Exception True True
except clauses are tested top to bottom and the first match wins; Exception matches every subclass, so the more specific AppError handler is unreachable.
More Python Programming questions
- 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)
- What is printed? python print(round(0.125, 2), round(2.675, 2), round(-1.5), round(25, -1), round(35, -1))