ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class E(Exception):
def __str__(self): return "custom"
try:
try:
raise E("orig")
except E as e:
raise TypeError("wrap") from e
except TypeError as t:
c = t.__cause__
print(type(c).__name__, str(c), c.args, t.__context__ is c)- A. E orig ('orig',) True
- B. E custom ('orig',) False
- C. TypeError wrap ('wrap',) True
- D. E custom ('orig',) True
Correct answer: D. E custom ('orig',) True
`raise ... from e` sets __cause__ to e; because it is raised inside the handler, __context__ is also e. The overridden __str__ controls str(), while args still holds the constructor argument.
More Python Programming questions
- 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)
- What is printed? python s = "abcdefghij" print(s[2:8:3], s[-2:-9:-3], s[8:2:3] == "", s[::4] + s[1::4])
- What is printed? python L = list(range(10)) L[2:8:2] = "XYZ" del L[-3:] L[len(L):] = [99] print(L)