Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
NumPy - Aggregation Functions
What is wrong with this code snippet?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = np.sum(arr, axis=2)
AThe array is not defined correctly.
Bnp.sum() cannot be used on 2D arrays.
CThe axis value 2 is invalid for a 2D array.
DThe import statement is missing.
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array has shape (2, 2), so valid axis values are 0 or 1.
  2. Step 2: Validate axis parameter

    axis=2 is out of bounds for this 2D array, causing an error.
  3. Final Answer:

    The axis value 2 is invalid for a 2D array. -> Option C
  4. Quick Check:

    Axis must be less than array dimensions [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using axis larger than array dimensions
  • Thinking np.sum can't handle 2D arrays
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes