Bird
0
0

The following code throws an error. What is the problem?

medium📝 Debug Q14 of 15
NumPy - Array Operations
The following code throws an error. What is the problem?
import numpy as np
arr = np.array([1, 2, 3])
result = np.add(arr, 5, 10)
print(result)
Anp.add cannot add a scalar to an array
BThe array must be of float type for np.add
Cnp.add takes only two arguments, but three were given
DThe print statement is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check np.add function signature

    np.add takes exactly two arguments: two arrays or an array and a scalar.
  2. Step 2: Identify the error in arguments

    Here, three arguments are passed: arr, 5, and 10, which is invalid.
  3. Final Answer:

    np.add takes only two arguments, but three were given -> Option C
  4. Quick Check:

    np.add = 2 args only [OK]
Quick Trick: np.add needs exactly two inputs, no extras [OK]
Common Mistakes:
  • Passing more than two arguments to np.add
  • Thinking np.add requires float arrays
  • Blaming print instead of function call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes