Bird
0
0

What is the output of this code?

medium📝 Predict Output Q4 of 15
NumPy - Array Operations
What is the output of this code?
import numpy as np
x = np.array([1, 2, 3])
y = x * 2
print(y)
A[1 2 3 1 2 3]
B[2 4 6]
C[3 4 5]
D[1 4 9]
Step-by-Step Solution
Solution:
  1. Step 1: Understand vectorized multiplication

    Multiplying array x by 2 multiplies each element by 2.
  2. Step 2: Calculate element-wise result

    Elements: 1*2=2, 2*2=4, 3*2=6, so result is [2 4 6].
  3. Final Answer:

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

    Vectorized multiply result = [2 4 6] [OK]
Quick Trick: Multiplying array by scalar doubles each element [OK]
Common Mistakes:
  • Confusing multiplication with concatenation
  • Expecting sum instead of product
  • Misreading output format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes