Bird
0
0

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

medium📝 Debug Q14 of 15
NumPy - Array Operations
The following code throws an error. What is the cause?
import numpy as np
arr = np.array([1, 2, 3])
result = arr + '5'
print(result)
ACannot add string scalar to numeric array
BArray is not defined
CMissing import statement
DSyntax error in addition
Step-by-Step Solution
Solution:
  1. Step 1: Identify data types involved in addition

    arr is numeric array, '5' is a string scalar. NumPy cannot add string to numbers.
  2. Step 2: Understand error cause

    Adding string to numeric array causes TypeError because types mismatch.
  3. Final Answer:

    Cannot add string scalar to numeric array -> Option A
  4. Quick Check:

    Scalar type must match array type [OK]
Quick Trick: Scalar must be number, not string, for numeric arrays [OK]
Common Mistakes:
  • Thinking import is missing
  • Confusing syntax error with type error
  • Assuming array is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes