Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Broadcasting
What is the output of the following code?
import numpy as np
shape = np.broadcast_shapes((3, 1), (1, 4))
print(shape)
A(1, 1)
B(3, 4)
C(3, 1, 4)
DError: shapes not compatible
Step-by-Step Solution
Solution:
  1. Step 1: Check compatibility of (3,1) and (1,4)

    Last dims: 1 and 4 compatible (one is 1), first dims: 3 and 1 compatible.
  2. Step 2: Determine broadcasted shape

    Broadcasted shape is max of each dimension: (3, 4).
  3. Final Answer:

    The output is (3, 4) -> Option B
  4. Quick Check:

    Broadcasted shape = (3, 4) [OK]
Quick Trick: Broadcast shape picks max dimension or 1 [OK]
Common Mistakes:
  • Expecting concatenation of shapes
  • Confusing shape tuple with array values
  • Assuming error when shapes differ

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes