ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def risky(n):
if n == 1: raise KeyError(n)
if n == 2: raise IndexError(n)
return n
res = []
for n in range(4):
try:
res.append(risky(n))
except LookupError as e:
res.append(type(e).__name__[0] + str(e))
print(res)- A. [0, 'K1', 'I2', 3, None]
- B. [0, 'K1', 'I2', 3]
- C. [0, "K'1'", 'I2', 3]
- D. [0, 'L1', 'L2', 3]
Correct answer: B. [0, 'K1', 'I2', 3]
KeyError and IndexError both derive from LookupError. str() of a KeyError gives the repr of its key, but for an int key that is just '1'.
More Python Programming questions
- What is printed? python class E(Exception): def str(self): return "custom" try: try: raise E("orig") except E as e: raise…
- 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])