Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q6 of 15
NumPy - Aggregation Functions
What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3])
result = np.prod(arr, axis=3)
print(result)
Anp.prod() does not accept an axis argument
BAxis 3 is out of bounds for a 1D array
CThe array must be 2D to use np.prod()
DThe print statement is missing parentheses
Step-by-Step Solution
Solution:
  1. Step 1: Check array dimensions

    The array arr is 1D with shape (3,).
  2. Step 2: Validate axis parameter

    Axis values must be within the array's dimensions. For 1D arrays, valid axis values are 0 or None.
  3. Step 3: Identify error

    Using axis=3 is invalid and causes an IndexError.
  4. Final Answer:

    Axis 3 is out of bounds for a 1D array -> Option B
  5. Quick Check:

    Axis must be less than array.ndim [OK]
Quick Trick: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using axis values larger than array dimensions
  • Assuming np.prod() requires 2D arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes