Consider the code:
arr = [3, 2, 3]
print(arr[3])
Which of the following exception is raised?
Consider the code:
arr = [3, 2, 3]
print(arr[3])
Which of the following exception is raised?
Solution
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 has a last index of .
Related questions:
2026: 24 May Shift 1
2025: 3 June Shift 2
2025: 20 May Shift 1
2026: 23 May Shift 2