ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def g():
    try:
        return 1
    finally:
        return 2
print(g())
  1. A. 2
  2. B. 1
  3. C. SyntaxError
  4. D. None

Correct answer: A. 2

A return in a finally block replaces the pending return value, so the function returns 2. Python allows this, though linters warn against it.

More Python Programming questions

Exams that ask this topic