Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Array Operations
What will be the output of the following code?
import numpy as np
arr = np.array([1, 2, 3])
arr *= 2
print(arr)
A[1 2 3]
B[2 4 6]
C[3 4 5]
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operation arr *= 2

    This multiplies each element of arr by 2 in-place, changing arr to [2, 4, 6].
  2. Step 2: Check the print output

    Printing arr shows the updated array [2 4 6].
  3. Final Answer:

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

    arr *= 2 doubles elements in-place [OK]
Quick Trick: *= doubles array elements in-place [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Confusing *= with creating new array
  • Thinking it causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes