Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Broadcasting
What is the output of the following code?
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([10, 20, 30])
C = A + B
print(C)
A[[11 22 33] [14 25 36]]
B[[11 22 33] [14 25 36] [10 20 30]]
C[[10 20 30] [10 20 30]]
DValueError due to shape mismatch
Step-by-Step Solution
Solution:
  1. Step 1: Understand shapes of A and B

    A has shape (2,3), B has shape (3,). B will broadcast across rows of A.
  2. Step 2: Perform element-wise addition

    Each row of A adds B: [1+10, 2+20, 3+30] = [11, 22, 33], [4+10, 5+20, 6+30] = [14, 25, 36].
  3. Final Answer:

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

    Broadcast adds vector to each matrix row [OK]
Quick Trick: Add vector to each matrix row when vector shape matches columns [OK]
Common Mistakes:
  • Expecting an extra row in output
  • Confusing broadcasting with stacking arrays
  • Thinking shape mismatch causes error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes