Bird
Raised Fist0
NumPydata~20 mins

View vs copy behavior in NumPy - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
View vs Copy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of modifying a NumPy slice?
Consider the following code where we create a NumPy array and then slice it. What will be the output after modifying the slice?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
slice_arr = arr[1:4]
slice_arr[0] = 10
print(arr)
A[ 1 2 3 4 5]
B[ 1 10 3 4 5]
C[ 1 10 2 4 5]
D[10 2 3 4 5]
Attempts:
2 left
💡 Hint
Remember that slicing a NumPy array creates a view, not a copy.
❓ Predict Output
intermediate
2:00remaining
What happens when using np.copy() on a slice?
Given the code below, what will be printed after modifying the copied slice?
NumPy
import numpy as np
arr = np.array([5, 6, 7, 8, 9])
copied_slice = np.copy(arr[1:4])
copied_slice[0] = 100
print(arr)
A[ 5 6 100 8 9]
B[ 5 100 7 8 9]
C[100 6 7 8 9]
D[ 5 6 7 8 9]
Attempts:
2 left
💡 Hint
np.copy() creates a new independent array.
❓ data_output
advanced
2:00remaining
How many elements are changed in the original array after this operation?
What is the number of elements in the original array 'arr' that are modified after running this code?
NumPy
import numpy as np
arr = np.arange(10)
view = arr[2:8:2]
view[:] = -1
print(arr)
A3
B6
C5
D4
Attempts:
2 left
💡 Hint
Check the slice indices and how many elements it covers.
🔧 Debug
advanced
2:00remaining
Why does this code not modify the original array?
Examine the code below. Why does modifying 'sub_arr' not change 'arr'?
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
sub_arr = arr[1:4].copy()
sub_arr[0] = 999
print(arr)
ABecause the slice indices are incorrect and no elements are selected.
BBecause arr is immutable and cannot be changed.
CBecause sub_arr is a copy, not a view, so changes don't affect arr.
DBecause sub_arr is a view but changes are buffered and not applied immediately.
Attempts:
2 left
💡 Hint
Check how sub_arr is created.
🚀 Application
expert
3:00remaining
Identify the correct way to modify a NumPy array without affecting the original
You want to modify a subset of a NumPy array without changing the original array. Which option correctly achieves this?
A
subset = arr[2:5].copy()
subset[0] = 100
B
subset = arr[2:5]
subset[0] = 100
C
subset = arr[2:5]
arr[2] = 100
D
subset = arr.copy()[2:5]
subset[0] = 100
Attempts:
2 left
💡 Hint
Think about which creates an independent copy before modification.

Practice

(1/5)
1. What happens when you create a view of a NumPy array and modify the view?
easy
A. The original array also changes because the view shares data.
B. The original array remains unchanged because the view is a copy.
C. An error occurs because views cannot be modified.
D. The view becomes independent and changes do not affect anything.

Solution

  1. Step 1: Understand what a view is in NumPy

    A view shares the same data buffer with the original array, so changes affect both.
  2. Step 2: Effect of modifying the view

    When you modify the view, the original array's data changes because they share memory.
  3. Final Answer:

    The original array also changes because the view shares data. -> Option A
  4. Quick Check:

    View shares data = original changes [OK]
Hint: Views share data; modifying view changes original [OK]
Common Mistakes:
  • Thinking views are independent copies
  • Assuming modifying a view raises an error
  • Confusing views with deep copies
2. Which of the following is the correct way to create a copy of a NumPy array named arr?
easy
A. copy_arr = arr.view()
B. copy_arr = arr
C. copy_arr = arr.copy()
D. copy_arr = arr[:]

Solution

  1. Step 1: Identify methods to create copies

    The copy() method creates a new independent array with its own data.
  2. Step 2: Differentiate from view and slicing

    view() creates a view, arr[:] creates a view (slice), and copy_arr = arr just assigns a reference.
  3. Final Answer:

    copy_arr = arr.copy() -> Option C
  4. Quick Check:

    copy() makes independent copy [OK]
Hint: Use copy() for independent array copy [OK]
Common Mistakes:
  • Using view() instead of copy()
  • Assuming slicing creates a copy
  • Assigning array without copy creates reference
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3, 4])
view_arr = arr[1:3]
view_arr[0] = 10
print(arr)
medium
A. [1 10 3 4]
B. [1 2 3 4]
C. [1 10 10 4]
D. [10 2 3 4]

Solution

  1. Step 1: Understand slicing creates a view

    The slice arr[1:3] creates a view sharing data with arr.
  2. Step 2: Modify the view and observe original array

    Changing view_arr[0] changes arr[1] to 10, so arr becomes [1, 10, 3, 4].
  3. Final Answer:

    [1 10 3 4] -> Option A
  4. Quick Check:

    Slicing = view, change affects original [OK]
Hint: Slicing creates view; changes reflect in original [OK]
Common Mistakes:
  • Thinking slicing creates a copy
  • Expecting original array unchanged
  • Modifying wrong index in view
4. The following code is intended to create an independent copy of arr and modify it without changing arr. What is the error?
import numpy as np
arr = np.array([5, 6, 7])
copy_arr = arr[::]
copy_arr[0] = 100
print(arr)
medium
A. You cannot modify arrays created by slicing.
B. The syntax for slicing is incorrect and causes an error.
C. The print statement is missing parentheses.
D. Slicing arr[::] creates a view, not a copy, so original changes.

Solution

  1. Step 1: Analyze slicing behavior

    Slicing with arr[::] creates a view, not a copy, so both share data.
  2. Step 2: Effect of modifying the slice

    Changing copy_arr[0] changes arr[0] as well, so original array is modified.
  3. Final Answer:

    Slicing arr[::] creates a view, not a copy, so original changes. -> Option D
  4. Quick Check:

    Slicing = view, not copy [OK]
Hint: Slicing creates view, not copy; use copy() for independence [OK]
Common Mistakes:
  • Assuming slicing makes a copy
  • Thinking slicing syntax is wrong
  • Believing modifying slice raises error
5. You have a large NumPy array data. You want to create a smaller array subset that you can modify freely without affecting data. Which approach is best and why?
import numpy as np
data = np.arange(1000)
subset = data[100:200]
subset[0] = -1
print(data[100])
hard
A. Use subset = data[100:200] because slicing creates a copy.
B. Use subset = data[100:200].copy() to create an independent copy.
C. Use subset = data.view()[100:200] to avoid copying data.
D. Assign subset = data and modify it directly.

Solution

  1. Step 1: Understand slicing creates a view

    Slicing data[100:200] creates a view sharing data with data.
  2. Step 2: Use copy() to avoid modifying original

    Calling copy() creates an independent array, so changes to subset don't affect data.
  3. Step 3: Check the effect of modification

    Without copy(), modifying subset[0] changes data[100]. With copy(), data[100] stays unchanged.
  4. Final Answer:

    Use subset = data[100:200].copy() to create an independent copy. -> Option B
  5. Quick Check:

    copy() makes independent array for safe modification [OK]
Hint: Use copy() on slice to avoid changing original array [OK]
Common Mistakes:
  • Assuming slicing creates independent copy
  • Using view() which shares data
  • Modifying original array unintentionally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard