Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q4 of Q15
Python - Exception Handling Fundamentals
What will be the output of this code?
try:
    a = [1, 2, 3]
    print(a[5])
except IndexError:
    print('Index error caught')
A3
BKeyError
Cnull
DIndex error caught
Step-by-Step Solution
Solution:
  1. Step 1: Analyze list access

    Accessing a[5] is out of range since list has only 3 elements (indices 0-2).
  2. Step 2: Identify exception and handling

    This raises IndexError, which is caught by except block printing 'Index error caught'.
  3. Final Answer:

    Index error caught -> Option D
  4. Quick Check:

    Out of range list index = IndexError caught [OK]
Quick Trick: Out-of-range list access raises IndexError [OK]
Common Mistakes:
MISTAKES
  • Expecting print of last element
  • Confusing with KeyError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes