Bird
0
0

What is the output of this code?

medium📝 Predict Output Q13 of 15
NumPy - Broadcasting
What is the output of this code?
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([10, 20])
result = x + y
print(result)
ABroadcasting error
B[[11 12 13] [14 15 16]]
C[[11 12 13] [24 25 26]]
D[[11 22 13] [14 25 16]]
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of x and y

    x shape is (2,3), y shape is (2,). These shapes are not compatible for broadcasting because trailing dimensions differ and y cannot be stretched to match x.
  2. Step 2: Predict result of x + y

    Since shapes are incompatible, NumPy raises a broadcasting error.
  3. Final Answer:

    Broadcasting error -> Option A
  4. Quick Check:

    Shape (2,3) + (2,) = error [OK]
Quick Trick: Match shapes from right; trailing dims must be equal or 1 [OK]
Common Mistakes:
  • Assuming (2,) broadcasts to (2,3)
  • Expecting element-wise addition without shape check
  • Ignoring broadcasting rules for 2D arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes