ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Err(Exception):
def __init__(self, code):
super().__init__(f"code {code}")
self.code = code
try:
try:
raise Err(7)
except Err as e:
e.code += 1
raise
except Exception as e:
print(e.code, e, e.args)- A. 8 code 8 ('code 8',)
- B. 7 code 7 ('code 7',)
- C. 8 code 7 (7,)
- D. 8 code 7 ('code 7',)
Correct answer: D. 8 code 7 ('code 7',)
A bare raise re-raises the same exception object, so the incremented attribute survives. The message and args were fixed when super().__init__ ran with 'code 7'.
More Python Programming questions
- What is printed? python def check(v): assert v > 0, f"bad {v}" return v try: check(-1) except AssertionError as e: print("AE", e, end=" ")…
- What is printed? python it = iter([1, 2]) try: while True: print(next(it), end=" ") except StopIteration as e: print("stop", e.value…
- What is printed? python def f(): try: return [1] finally: pass def g(): x = [1] try: return x finally: x.append(2) def h(): x = 1 try…
- What is printed? (Output lines are shown separated by " / ".) python class P: def init(self, x): self.x = x def eq(self, o): return self.x…
- What is printed? python class V: def init(self, n): self.n = n def add(self, o): return V(self.n + o) def radd(self, o): return V(o 10 +…
- What is printed? python class R: def init(self, n): self.n = n def len(self): return self.n def getitem(self, i): if i >= self.n: raise…