ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
try:
    try:
        raise ValueError
    except TypeError:
        print("T", end=" ")
    finally:
        print("F1", end=" ")
except ValueError:
    print("V", end=" ")
else:
    print("E", end=" ")
finally:
    print("F2")
  1. A. V F1 F2
  2. B. F1 V E F2
  3. C. T F1 F2
  4. D. F1 V F2

Correct answer: D. F1 V F2

The inner except TypeError does not match the ValueError, so the inner finally prints F1 and the exception moves outward. The outer except ValueError prints V. else runs only when NO exception happened, so it is skipped, and the outer finally prints F2.

More Python Programming questions

Exams that ask this topic