Bird
0
0

Consider the code:

medium📝 Predict Output Q13 of 15
NumPy - Indexing and Slicing
Consider the code:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
slice_arr = arr[1:4]
slice_arr[0] = 99
print(arr)

What is the output?
A[10 99 30 40 50]
B[10 20 30 40 50]
C[10 99 99 40 50]
D[10 20 99 40 50]
Step-by-Step Solution
Solution:
  1. Step 1: Understand slice is a view

    The slice arr[1:4] returns a view sharing data with arr. Changing slice_arr[0] changes arr[1].
  2. Step 2: Apply the change and print

    Setting slice_arr[0] = 99 updates arr[1] to 99. The rest of arr remains unchanged.
  3. Final Answer:

    [10 99 30 40 50] -> Option A
  4. Quick Check:

    View change updates original array [OK]
Quick Trick: Changing slice changes original array if no copy [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Changing wrong indices
  • Confusing slice indices with original

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes