Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
NumPy - Indexing and Slicing
What is wrong with this code snippet?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr[2, 0])
APrints 3 correctly
BSyntaxError due to wrong brackets
CIndexError because row 2 does not exist
DPrints 1 correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check array shape and indexes

    The array has 2 rows (index 0 and 1). Index 2 for row is out of range.
  2. Step 2: Understand error type

    Accessing arr[2, 0] causes an IndexError because row 2 does not exist.
  3. Final Answer:

    IndexError because row 2 does not exist -> Option C
  4. Quick Check:

    Row index 2 is invalid for 2-row array [OK]
Quick Trick: Check array shape before indexing to avoid errors [OK]
Common Mistakes:
  • Assuming indexing starts at 1
  • Ignoring array shape
  • Confusing error types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes