ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def check(v):
assert v > 0, f"bad {v}"
return v
try:
check(-1)
except AssertionError as e:
print("AE", e, end=" ")
except Exception:
print("other", end=" ")
print(isinstance(AssertionError(), Exception))- A. other True
- B. AE bad -1 True
- C. AE True
- D. AE bad -1 False
Correct answer: B. AE bad -1 True
A failing assert raises AssertionError with the message expression as its argument (when run without -O). AssertionError is an Exception subclass, but the first matching clause wins.
More Python Programming questions
- 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…
- What is printed? python class C: def init(self): self.d = {} def getattr(self, name): return f"<{name}>" def setattr(self, name, v)…