Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
NumPy - Indexing and Slicing
Identify the error in the following code snippet:
import numpy as np
arr = np.array([5, 10, 15, 20])
indices = [1, 4]
print(arr[indices])
AIndexError because index 4 is out of bounds
BSyntaxError due to wrong brackets
CTypeError because indices is a list
DNo error, prints [10 20]
Step-by-Step Solution
Solution:
  1. Step 1: Check array size and indices

    Array length is 4, valid indices are 0 to 3. Index 4 is invalid.
  2. Step 2: Understand error type

    Accessing index 4 causes IndexError: index out of bounds.
  3. Final Answer:

    IndexError because index 4 is out of bounds -> Option A
  4. Quick Check:

    Index 4 invalid for length 4 array [OK]
Quick Trick: Check if all indices are within array length [OK]
Common Mistakes:
  • Assuming list indices are always valid
  • Confusing syntax errors with index errors
  • Thinking list indices cause type errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes