Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - Exception Handling Fundamentals
What will be the output of this code?
try:
    numbers = [1, 2, 3]
    print(numbers[5])
except IndexError:
    print('Index error caught')
ANo output
B3
C5
DIndex error caught
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code behavior

    The code tries to print the element at index 5, but the list has only 3 elements (indexes 0 to 2).
  2. Step 2: Identify the exception and handling

    Accessing index 5 causes IndexError, which is caught by the except block that prints 'Index error caught'.
  3. Final Answer:

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

    IndexError triggers except block output [OK]
Quick Trick: Accessing invalid list index causes IndexError [OK]
Common Mistakes:
  • Expecting the code to print 3 or 5
  • Ignoring the except block catching the error
  • Thinking no output will occur

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes