Skip to main contentSkip to solution

Consider the code:

arr = [3, 2, 3]

print(arr[3])

Which of the following exception is raised?

Solution

✅ Correct Option: 1

The list arr = [3, 2, 3] has 3 elements, so its valid indices are 0, 1 and 2 (or -1, -2, -3). Accessing arr[3] goes past the last element, so Python raises an IndexError: list index out of range.


Syntax Error: raised when the code is written incorrectly (like a missing bracket), before it runs. Here the code is valid, and the error appears only at run time.

Value Error: raised when a function gets the right type of argument but an unsuitable value (like int("abc")), not for a bad index.

Indexing starts at 0, so a list of length nn has a last index of n−1n-1.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question