0
0
NumPydata~20 mins

Garbage collection and array references in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Garbage Collection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of modifying a view on the original array

Consider the following code snippet using NumPy arrays:

import numpy as np
arr = np.array([1, 2, 3, 4])
view_arr = arr[1:3]
view_arr[0] = 10
print(arr)

What will be the output of the print statement?

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
view_arr = arr[1:3]
view_arr[0] = 10
print(arr)
A[1 10 3 4]
B[1 2 3 4]
C[10 2 3 4]
D[1 10 10 4]
Attempts:
2 left
💡 Hint

Think about whether view_arr is a copy or a view of arr.

query_result
intermediate
2:00remaining
Effect of modifying a copy on the original array

What will be the output of this code?

import numpy as np
arr = np.array([5, 6, 7, 8])
copy_arr = arr.copy()
copy_arr[1] = 20
print(arr)
NumPy
import numpy as np
arr = np.array([5, 6, 7, 8])
copy_arr = arr.copy()
copy_arr[1] = 20
print(arr)
A[20 6 7 8]
B[5 6 7 8]
C[5 20 7 8]
D[5 6 20 8]
Attempts:
2 left
💡 Hint

Does modifying a copy affect the original array?

📝 Syntax
advanced
2:00remaining
Identifying reference count changes with array assignment

Given the following code, which statement correctly describes the reference count behavior?

import numpy as np
arr = np.array([1, 2, 3])
ref1 = arr
ref2 = arr
ref1[0] = 10
print(arr)
NumPy
import numpy as np
arr = np.array([1, 2, 3])
ref1 = arr
ref2 = arr
ref1[0] = 10
print(arr)
Aref1 is a copy; modifying it does not affect arr or ref2.
Bref2 is a copy; modifying ref1 affects arr but not ref2.
Carr is copied when assigned to ref1 and ref2; changes are independent.
DAll variables point to the same array; modifying ref1 changes arr and ref2.
Attempts:
2 left
💡 Hint

Think about what happens when you assign arrays to new variables without copying.

🔧 Debug
advanced
2:00remaining
Why does the original array change unexpectedly?

Consider this code:

import numpy as np
arr = np.array([1, 2, 3, 4])
new_arr = arr[::2]
new_arr[0] = 100
print(arr)

Why does arr change even though slicing looks like it creates a new array?

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
new_arr = arr[::2]
new_arr[0] = 100
print(arr)
AThe assignment to new_arr creates a new array independent of arr.
BSlicing always creates a copy, so arr should not change.
CSlicing with steps creates a view, so changes affect the original array.
DThe print statement is incorrect and does not show the real arr.
Attempts:
2 left
💡 Hint

Check if slicing with steps returns a view or a copy.

🧠 Conceptual
expert
2:00remaining
Garbage collection and array references in NumPy

Which statement best describes how Python's garbage collector handles NumPy arrays with multiple references?

AThe array memory is freed only when all references to it are deleted or go out of scope.
BThe array memory is freed immediately when a new reference is created.
CNumPy arrays are never garbage collected and always stay in memory.
DGarbage collection frees arrays as soon as any reference is deleted, regardless of others.
Attempts:
2 left
💡 Hint

Think about how reference counting works in Python.