Bird
0
0

What error will be raised by the following code?

medium📝 Debug Q6 of 15
NumPy - Array Manipulation
What error will be raised by the following code?
import numpy as np
arr = np.arange(14)
new_arr = arr.reshape(3, 5)
ATypeError: reshape expects integers as arguments
BValueError: cannot reshape array of size 14 into shape (3,5)
CIndexError: index out of bounds
DNo error, code runs successfully
Step-by-Step Solution
Solution:
  1. Step 1: Check total elements

    Original array has 14 elements (0 to 13).
  2. Step 2: Check new shape size

    New shape (3, 5) requires 15 elements.
  3. Step 3: Understand reshape constraints

    Reshape requires total elements to remain constant.
  4. Final Answer:

    ValueError: cannot reshape array of size 14 into shape (3,5) -> Option B
  5. Quick Check:

    Element count mismatch causes ValueError [OK]
Quick Trick: Total elements must match in reshape [OK]
Common Mistakes:
  • Ignoring total element count when reshaping
  • Assuming reshape can add or remove elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes