What happens to a NumPy array when all references to it are deleted?
The array's memory is freed by Python's garbage collector because no references remain to it.
Click to reveal answer
beginner
How does assigning one NumPy array to another variable affect memory?
Both variables point to the same array in memory; no new array is created unless explicitly copied.
Click to reveal answer
beginner
What method can you use to create a separate copy of a NumPy array to avoid shared references?
Use the copy() method, e.g., new_array = old_array.copy().
Click to reveal answer
intermediate
Why is it important to understand references when working with NumPy arrays?
Because modifying one variable can change the array seen by another variable if they share the same reference.
Click to reveal answer
intermediate
How does Python's garbage collector know when to free a NumPy array's memory?
It tracks reference counts; when the count drops to zero, the memory is released.
Click to reveal answer
If you assign b = a where a is a NumPy array, what happens?
A<code>b</code> points to the same array as <code>a</code>.
BA new array is created and stored in <code>b</code>.
CThe array is deleted from memory.
DPython raises an error.
✗ Incorrect
Assigning b = a makes b reference the same array as a. No new array is created.
Which method creates a new independent copy of a NumPy array?
A<code>array.duplicate()</code>
B<code>array.assign()</code>
C<code>array.clone()</code>
D<code>array.copy()</code>
✗ Incorrect
The copy() method creates a new array with its own memory.
What triggers Python's garbage collector to free a NumPy array's memory?
AWhen the array is explicitly deleted with <code>del</code>.
BWhen no references to the array remain.
CWhen the array is larger than 1GB.
DWhen the program ends.
✗ Incorrect
Memory is freed when the reference count drops to zero, meaning no variables point to the array.
If you modify a NumPy array through one variable, what happens to other variables referencing the same array?
AThey see the updated changes.
BThey keep the old values.
CThey raise an error.
DThey become None.
✗ Incorrect
Since they reference the same array, changes are visible through all variables.
Which of the following is NOT true about NumPy array references?
AMultiple variables can point to the same array.
BUsing <code>copy()</code> creates a new array in memory.
CAssigning one array to another variable copies the data automatically.
DGarbage collection frees arrays with zero references.
✗ Incorrect
Assigning one variable to another does NOT copy data; it just creates a new reference.
Explain how Python's garbage collection works with NumPy arrays and why references matter.
Think about how Python tracks variables pointing to the same array.
You got /4 concepts.
Describe the difference between assigning a NumPy array to a new variable and creating a copy of it.
Consider what happens in memory and when you change the array.
You got /4 concepts.
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
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.
Step 2: Effect of shared references
Both variables point to the same memory location, so changes via one variable affect the other.
Final Answer:
Both variables refer to the same array data in memory. -> Option C
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
Step 1: Identify the method for copying arrays
The .copy() method creates a new independent array with its own data.
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.
Final Answer:
b = a.copy() -> Option A
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
Step 1: Understand reference assignment
Variable y is assigned to x, so both refer to the same array.
Step 2: Effect of modifying y on x
Changing y[0] to 10 changes the shared array, so x[0] is also 10.
Final Answer:
[10 2 3] -> Option A
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
Step 1: Understand slicing behavior in NumPy
Slicing an array creates a view, not a copy, so y shares data with x.
Step 2: Effect of modifying y on x
Changing y[0] to 5 modifies the shared data, so x[0] also becomes 5.
Final Answer:
Slicing creates a view, so modifying y changes x. -> Option D
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
Step 1: Understand Python garbage collection
Python frees memory of objects when no references to them remain.
Step 2: Effect of references on NumPy arrays
NumPy arrays are freed automatically when all variables referencing them are deleted or reassigned.
Final Answer:
Deleting all references to a allows Python to free its memory automatically. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.