Bird
0
0

Which of the following is the correct syntax to add 5 to every element of a numpy array arr in-place?

easy📝 Syntax Q12 of 15
NumPy - Array Operations
Which of the following is the correct syntax to add 5 to every element of a numpy array arr in-place?
Aarr += 5
Barr = arr + 5
Carr.add(5)
Darr = arr.add(5)
Step-by-Step Solution
Solution:
  1. Step 1: Identify in-place addition syntax

    The operator += modifies the array in-place by adding a value.
  2. Step 2: Compare options

    arr = arr + 5 creates a new array, options C and D are invalid numpy syntax for in-place add.
  3. Final Answer:

    arr += 5 -> Option A
  4. Quick Check:

    Use += for in-place addition [OK]
Quick Trick: Use += to add in-place, not = arr + value [OK]
Common Mistakes:
  • Using = arr + 5 which creates a new array
  • Trying arr.add(5) which is not a numpy method
  • Assigning arr = arr.add(5) which causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes