Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
NumPy - Broadcasting
What will be the output of this code?
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([10, 20, 30])
print(x + y)
A[[11 22 33] [14 25 36] [10 20 30]]
B[[10 20 30] [10 20 30]]
CBroadcasting error
D[[11 22 33] [14 25 36]]
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of x and y

    x shape is (2,3), y shape is (3,). Trailing dimensions match.
  2. Step 2: Apply broadcasting rules

    y broadcasts to each row of x, adding element-wise.
  3. Final Answer:

    [[11 22 33] [14 25 36]] -> Option D
  4. Quick Check:

    Broadcasting adds y to each row of x [OK]
Quick Trick: Trailing dimension match allows row-wise addition [OK]
Common Mistakes:
  • Expecting error due to different array dimensions
  • Misreading output shape
  • Confusing broadcasting with stacking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes