Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Broadcasting
What will be the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = arr * 2
print(result)
A[[1 2] [3 4] [2 4] [6 8]]
B[[2 4] [6 8]]
C[[3 4] [5 6]]
DError: cannot multiply 2D array by scalar
Step-by-Step Solution
Solution:
  1. Step 1: Recognize scalar multiplication with 2D array

    Scalar 2 multiplies each element of the 2D array.
  2. Step 2: Calculate element-wise multiplication

    [[1*2, 2*2], [3*2, 4*2]] = [[2, 4], [6, 8]]
  3. Final Answer:

    [[2 4] [6 8]] -> Option B
  4. Quick Check:

    Scalar multiplies each element in 2D array [OK]
Quick Trick: Scalar multiplies all elements in any array shape [OK]
Common Mistakes:
  • Expecting error on 2D arrays
  • Confusing multiplication with concatenation
  • Misreading output shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes