ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
def f():
    try:
        return 1
    finally:
        return 2

print(f())
  1. A. 1
  2. B. None
  3. C. SyntaxError
  4. D. 2

Correct answer: D. 2

finally runs even after a return in try, and a return inside finally replaces the pending return value, so 2 is returned. (Python 3.14 adds a SyntaxWarning for this pattern, but it still runs.)

More Python Programming questions

Exams that ask this topic