Bird
0
0

What error will occur when running this code?

medium📝 Debug Q7 of 15
NumPy - Indexing and Slicing
What error will occur when running this code?
import numpy as np
arr = np.array([[7, 8], [9, 10]])
print(arr[1, 2])
AValueError due to invalid array shape
BTypeError because arr is not subscriptable
CIndexError because column index 2 is out of bounds
DNo error, prints 10
Step-by-Step Solution
Solution:
  1. Step 1: Check array shape

    arr has shape (2, 2), meaning valid indices for columns are 0 and 1.
  2. Step 2: Analyze index used

    Accessing arr[1, 2] tries to access the third column (index 2) which does not exist.
  3. Final Answer:

    IndexError because column index 2 is out of bounds -> Option C
  4. Quick Check:

    Indexing beyond array dimensions raises IndexError. [OK]
Quick Trick: Indices must be within array shape bounds [OK]
Common Mistakes:
  • Assuming indexing starts at 1 instead of 0
  • Confusing TypeError with IndexError
  • Believing the code prints the last element

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes