Bird
0
0

Find the mistake in this code snippet:

medium📝 Debug Q7 of 15
NumPy - Indexing and Slicing
Find the mistake in this code snippet:
import numpy as np
arr = np.array([10, 20, 30, 40])
indices = [1, 2, 4]
print(arr[indices])
AArray must be reshaped before indexing
BList cannot be used for indexing
CMissing import statement
DIndex 4 is out of bounds for the array
Step-by-Step Solution
Solution:
  1. Step 1: Check array length and indices

    arr length is 4, valid indices are 0 to 3. Index 4 is invalid.
  2. Step 2: Identify error cause

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

    Index 4 is out of bounds for the array -> Option D
  4. Quick Check:

    Index error due to invalid index = Index 4 is out of bounds for the array [OK]
Quick Trick: Indices must be less than array length [OK]
Common Mistakes:
  • Thinking list is invalid for indexing
  • Assuming reshape needed
  • Missing import is not the issue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes