Bird
0
0

Find the error in this code:

medium📝 Debug Q6 of 15
NumPy - Array Operations
Find the error in this code:
import numpy as np
arr = np.array([1, 2, 3])
result = arr * 2
print(result[3])
ATypeError: cannot multiply array by int
BSyntaxError: invalid syntax
CIndexError: index 3 is out of bounds
DNo error, prints 6
Step-by-Step Solution
Solution:
  1. Step 1: Check array size after multiplication

    Multiplying by 2 keeps array length 3: [2, 4, 6].
  2. Step 2: Accessing index 3 causes error

    Index 3 is out of range (valid indices: 0,1,2), causing IndexError.
  3. Final Answer:

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

    Accessing invalid index = IndexError [OK]
Quick Trick: Array indices start at 0; max index is length-1 [OK]
Common Mistakes:
  • Assuming multiplication changes array size
  • Expecting no error on out-of-range index
  • Confusing TypeError with IndexError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes