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
arr = np.array([1, 2, 3])
result = arr * 3
print(result)
A[3 3 3]
B[1 2 3 3 6 9]
CError due to shape mismatch
D[3 6 9]
Step-by-Step Solution
Solution:
  1. Step 1: Understand scalar multiplication with array

    Multiplying an array by a scalar multiplies each element by that scalar.
  2. Step 2: Calculate element-wise multiplication

    Each element in [1, 2, 3] is multiplied by 3, resulting in [3, 6, 9].
  3. Final Answer:

    [3 6 9] -> Option D
  4. Quick Check:

    Array * scalar = element-wise multiplication [OK]
Quick Trick: Multiply each element by scalar directly [OK]
Common Mistakes:
  • Expecting concatenation instead of multiplication
  • Thinking output is repeated elements
  • Assuming error due to shape mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes