Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Array Manipulation
Find the error in this code snippet:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat = arr.flatten
print(flat[0])
Aflatten() returns a view, not a copy
Bflatten is not called as a method
Carr is not a NumPy array
Dprint syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Identify method call mistake

    The code assigns flat = arr.flatten without parentheses, so flat is a method, not the flattened array.
  2. Step 2: Correct usage

    Trying to index flat[0] fails because flat is a method object, not an array. Use arr.flatten() directly.
  3. Final Answer:

    flatten is not called as a method when assigned. -> Option B
  4. Quick Check:

    Method must be called with () to execute [OK]
Quick Trick: Always use parentheses to call methods [OK]
Common Mistakes:
  • Assigning method without parentheses
  • Expecting flatten to be a property
  • Confusing method reference with method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes