ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def f():
try:
raise ValueError("a")
except ValueError as e:
err = e
return err
x = f()
print(type(x).__name__, x.args, str(x), repr(x))- A. ValueError ('a',) a a
- B. ValueError ('a',) ValueError('a') ValueError('a')
- C. UnboundLocalError
- D. ValueError ('a',) a ValueError('a')
Correct answer: D. ValueError ('a',) a ValueError('a')
The `as e` name is deleted at the end of the handler, but err still refers to the exception object, so no UnboundLocalError. args is a tuple, str() gives just the message and repr() shows the constructor form.
More Python Programming questions
- 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))
- What is printed? python print(int("٤٢"), "²".isdigit(), "²".isdecimal(), "½".isnumeric(), "4.2".isdigit())