Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Exception Handling Fundamentals
What will be the output of this code?
try:
    print('X')
except ValueError:
    print('Y')
else:
    print('Z')
AY\nZ
BX\nY
CX\nZ
DZ
Step-by-Step Solution
Solution:
  1. Step 1: Check for exceptions

    Prints 'X' in try; no exception occurs, so except block is skipped.
  2. Step 2: Else block runs only if no exception

    Since no exception, else block prints 'Z'.
  3. Final Answer:

    X\nZ -> Option C
  4. Quick Check:

    No exception = else runs after try [OK]
Quick Trick: Else runs only if try has no exception [OK]
Common Mistakes:
MISTAKES
  • Thinking except runs without exception
  • Ignoring else block
  • Assuming else runs always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes