Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Array Operations
What will be the output of the following code?
import numpy as np
arr = np.array([2, 4, 6])
arr += 5
print(arr)
A[2 4 6]
B[7 9 11]
C[5 5 5]
DError: unsupported operand
Step-by-Step Solution
Solution:
  1. Step 1: Understand '+=' operation on numpy array

    The '+=' operator adds 5 to each element in-place, modifying arr.
  2. Step 2: Calculate new values

    Original array [2,4,6] plus 5 becomes [7,9,11].
  3. Final Answer:

    [7 9 11] -> Option B
  4. Quick Check:

    In-place addition updates array values [OK]
Quick Trick: '+=' adds in-place, changing original array [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Thinking it creates new array
  • Confusing syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes