Bird
0
0

Identify the issue in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Array Operations
Identify the issue in this code snippet:
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([5, 6, 7])
z = x + y
print(z)
ASyntax error in numpy import
BArrays have incompatible shapes for element-wise addition
CUsing '+' operator does not perform element-wise addition
DArrays must be of integer type to add
Step-by-Step Solution
Solution:
  1. Step 1: Check array shapes

    x has shape (4,), y has shape (3,)
  2. Step 2: Understand broadcasting rules

    Shapes are incompatible for element-wise addition without broadcasting
  3. Step 3: Result

    Raises a ValueError due to shape mismatch
  4. Final Answer:

    Arrays have incompatible shapes for element-wise addition -> Option B
  5. Quick Check:

    Check array lengths differ [OK]
Quick Trick: Array shapes must align or broadcast for element-wise ops [OK]
Common Mistakes:
  • Assuming arrays of different lengths add element-wise
  • Ignoring broadcasting rules
  • Confusing syntax errors with shape errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes