Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - Exception Handling Fundamentals
What will be the output of this code?
my_list = [1, 2, 3]
print(my_list[3])
AIndexError
B3
CNone
DValueError
Step-by-Step Solution
Solution:
  1. Step 1: Understand list indexing

    List indices start at 0, so valid indices for my_list are 0, 1, 2.
  2. Step 2: Accessing index 3

    Index 3 is out of range, so Python raises an IndexError.
  3. Final Answer:

    IndexError -> Option A
  4. Quick Check:

    Out of range index = IndexError [OK]
Quick Trick: Accessing invalid list index raises IndexError [OK]
Common Mistakes:
  • Thinking it returns last element
  • Assuming None is returned for invalid index
  • Confusing IndexError with ValueError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes