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))
  1. A. ValueError ('a',) a a
  2. B. ValueError ('a',) ValueError('a') ValueError('a')
  3. C. UnboundLocalError
  4. 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

Exams that ask this topic