Bird
0
0

Identify the error in this code:

medium📝 Debug Q7 of 15
NumPy - Array Operations
Identify the error in this code:
import numpy as np
arr = np.array([1, 2, 3])
result = arr >= 2
print(result[3])
ASyntaxError due to wrong comparison operator
BIndexError because index 3 is out of bounds
CTypeError because result is not subscriptable
DNo error, prints True or False
Step-by-Step Solution
Solution:
  1. Step 1: Check array length and indexing

    arr has length 3, so result also has length 3 with indices 0,1,2.
  2. Step 2: Accessing index 3 causes error

    Accessing result[3] is out of bounds, causing IndexError.
  3. Final Answer:

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

    Array index must be within bounds [OK]
Quick Trick: Array indices start at 0 and max is length-1 [OK]
Common Mistakes:
  • Accessing index equal to array length
  • Confusing syntax errors with runtime errors
  • Assuming result is scalar

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes