ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def f():
    try:
        raise KeyError("k")
    finally:
        return "swallowed"
try:
    print(f(), end=" ")
except KeyError:
    print("KeyError", end=" ")
print("end")
  1. A. swallowed end
  2. B. KeyError end
  3. C. swallowed KeyError end
  4. D. end

Correct answer: A. swallowed end

A return inside finally cancels the exception that is propagating, so f() returns normally and the KeyError is silently lost.

More Python Programming questions

Exams that ask this topic