The array has 2 rows (index 0 and 1). Index 2 for row is out of range.
Step 2: Understand error type
Accessing arr[2, 0] causes an IndexError because row 2 does not exist.
Final Answer:
IndexError because row 2 does not exist -> Option C
Quick Check:
Row index 2 is invalid for 2-row array [OK]
Hint: Check array shape before indexing to avoid errors [OK]
Common Mistakes:
Assuming indexing starts at 1
Ignoring array shape
Confusing error types
5. You have a 4x4 numpy array arr filled with numbers 1 to 16 in row-major order. Which code correctly changes the element in the last row and second column to 100?
hard
A. arr[3, -2] = 100
B. arr[4, 2] = 100
C. arr[-1, 2] = 100
D. arr[3, 1] = 100
Solution
Step 1: Identify last row and second column indexes
For a 4x4 array, rows and columns are indexed 0 to 3. Last row is index 3, second column is index 1.
Step 2: Check each option
arr[3, 1] = 100 uses arr[3, 1] which is correct. arr[4, 2] = 100 uses invalid index 4. arr[-1, 2] = 100 uses column index 2 (third column), not second. arr[3, -2] = 100 uses negative index -2 for column which is the third column, not second.
Final Answer:
arr[3, 1] = 100 -> Option D
Quick Check:
Last row = 3, second column = 1 [OK]
Hint: Use zero-based indexes: last row is size-1, second column is 1 [OK]
Common Mistakes:
Using out-of-range indexes like 4
Confusing negative indexes for columns
Mixing row and column positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.