Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you assign one NumPy array to another variable without using .copy()?
easy
A. The array data is converted to a list.
B. A new independent copy of the array is created.
C. Both variables refer to the same array data in memory.
D. The original array is deleted from memory.

Solution

  1. Step 1: Understand assignment behavior in NumPy

    Assigning one array to another variable without .copy() does not create a new array but a reference to the same data.
  2. Step 2: Effect of shared references

    Both variables point to the same memory location, so changes via one variable affect the other.
  3. Final Answer:

    Both variables refer to the same array data in memory. -> Option C
  4. Quick Check:

    Assignment without copy = shared reference [OK]
Hint: Assignment copies reference, not data, unless .copy() used [OK]
Common Mistakes:
  • Thinking assignment creates a new array copy
  • Assuming changes to one variable don't affect the other
  • Confusing assignment with .copy() method
2. Which of the following is the correct syntax to create an independent copy of a NumPy array a?
easy
A. b = a.copy()
B. b = a
C. b = copy(a)
D. b = a[:]

Solution

  1. Step 1: Identify the method for copying arrays

    The .copy() method creates a new independent array with its own data.
  2. Step 2: Check other options

    Assignment b = a shares data; copy(a) is invalid without import; slicing a[:] creates a view, not a full copy.
  3. Final Answer:

    b = a.copy() -> Option A
  4. Quick Check:

    Use .copy() for independent array [OK]
Hint: Use .copy() method to avoid shared data [OK]
Common Mistakes:
  • Using simple assignment expecting a copy
  • Using slicing which creates a view, not a copy
  • Calling copy() without importing copy module
3. Consider the code:
import numpy as np
x = np.array([1, 2, 3])
y = x
y[0] = 10
print(x)

What is the output?
medium
A. [10 2 3]
B. [1 2 3]
C. [0 2 3]
D. Error: assignment to y does not affect x

Solution

  1. Step 1: Understand reference assignment

    Variable y is assigned to x, so both refer to the same array.
  2. Step 2: Effect of modifying y on x

    Changing y[0] to 10 changes the shared array, so x[0] is also 10.
  3. Final Answer:

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

    Shared reference means change reflects in both [OK]
Hint: Modifying reference changes original array if no copy [OK]
Common Mistakes:
  • Expecting original array to remain unchanged
  • Confusing assignment with copy
  • Thinking print will show error
4. What is wrong with this code snippet?
import numpy as np
x = np.array([1, 2, 3])
y = x[:]
y[0] = 5
print(x)
medium
A. Slicing creates a copy, so x remains unchanged.
B. Syntax error in slicing syntax.
C. y is not defined before assignment.
D. Slicing creates a view, so modifying y changes x.

Solution

  1. Step 1: Understand slicing behavior in NumPy

    Slicing an array creates a view, not a copy, so y shares data with x.
  2. Step 2: Effect of modifying y on x

    Changing y[0] to 5 modifies the shared data, so x[0] also becomes 5.
  3. Final Answer:

    Slicing creates a view, so modifying y changes x. -> Option D
  4. Quick Check:

    Slicing = view, not copy [OK]
Hint: Slicing shares data; use .copy() for independent array [OK]
Common Mistakes:
  • Assuming slicing creates a copy
  • Expecting original array to stay unchanged
  • Thinking slicing causes syntax error
5. You have a large NumPy array a used in multiple variables. You want to free memory used by a when no longer needed. Which statement is TRUE about garbage collection in this context?
hard
A. You must manually call a special function to free NumPy array memory.
B. Deleting all references to a allows Python to free its memory automatically.
C. NumPy arrays never get garbage collected automatically.
D. Assigning a = None immediately frees the memory even if other references exist.

Solution

  1. Step 1: Understand Python garbage collection

    Python frees memory of objects when no references to them remain.
  2. Step 2: Effect of references on NumPy arrays

    NumPy arrays are freed automatically when all variables referencing them are deleted or reassigned.
  3. Final Answer:

    Deleting all references to a allows Python to free its memory automatically. -> Option B
  4. Quick Check:

    Garbage collection frees unreferenced arrays [OK]
Hint: Remove all references to free array memory automatically [OK]
Common Mistakes:
  • Thinking manual memory free is needed
  • Assuming assigning None frees memory if other refs exist
  • Believing NumPy arrays are exempt from garbage collection
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