Bird
0
0

What is the issue with this code?

medium📝 Debug Q7 of 15
NumPy - Array Operations
What is the issue with this code?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
print(x + y)
AThe '+' operator performs matrix multiplication here
BYou cannot add numpy arrays
CThe arrays must be converted to lists first
DArrays have incompatible shapes for broadcasting
Step-by-Step Solution
Solution:
  1. Step 1: Check array shapes

    x has shape (3,), y has shape (2,). Shapes differ and cannot be broadcasted.
  2. Step 2: Understand broadcasting rules

    Arrays must have compatible shapes to add element-wise.
  3. Final Answer:

    Arrays have incompatible shapes for broadcasting -> Option D
  4. Quick Check:

    Shapes must align or broadcast [OK]
Quick Trick: Array shapes must be compatible for addition [OK]
Common Mistakes:
  • Thinking '+' does matrix multiplication
  • Believing numpy arrays can't be added
  • Trying to convert arrays to lists unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes