Python Memory Management Quiz
Progress
Score: 0/20
๐ฉ Master Quiz: Memory Management
20 Questions based on the "Warehousing & Cleaning Crew" analogy. Green = Correct, Red = Incorrect.
BeginnerAnalogy
In the Warehouse analogy, what corresponds to the "Heap Memory"?โ
- **A) The Shelves (where products live)
** - **B) The Catalog (sticky labels)
** - **C) The Cleaning Crew
** - **D) The Manager
**
๐ก Explanationโ
The Shelves (Data storage).
The Heap is the physical area where objects (products) are stored. The Stack is just the catalog of references (labels).
BeginnerVariable Creation
What happens in memory when you run x = 10?โ
- **A) It puts 10 inside a box named x (like C++)
** - **B) It writes 10 directly to the hard drive
** - **C) It creates object 10 in Heap, and name x in Stack
** - **D) It reserves 4 bytes of Stack memory
**
๐ก Explanationโ
Two-step process: Heap object, Stack reference.
Python creates the object 10 first (if not cached) in the Heap, then creates the label x in the Stack pointing to it.
IntermediateReferences
What happens if you run x = y?โ
- **A) It creates a copy of the object
** - **B) It copies the pointer (reference)
** - **C) It deletes x and replaces it with y
** - **D) It merges the two objects
**
๐ก Explanationโ
It copies the pointer.
Both variables end up pointing to the exact same object in the Heap. No new object is created.
ArchitectGarbage Collection
What is Python's Primary mechanism for Garbage Collection?โ
- **A) Reference Counting
** - **B) Generational Collection
** - **C) Tracing
** - **D) Manual Deletion
**
๐ก Explanationโ
Reference Counting.
This is real-time. As soon as the count hits zero (no labels left), the object is destroyed immediately.
AdvancedCyclic References
Why do we need a "Secondary" GC mechanism (Generational GC)?โ
- **A) To delete large files
** - **B) Because ref counting is too slow
** - **C) To handle Cyclic References
** - **D) To clear the Stack
**
๐ก Explanationโ
To handle Cyclic References.
If Object A points to B, and B points to A, their ref count never hits zero. The Generational GC scans for these isolated loops.
AdvancedGenerations
How many "Generations" does the Cyclic Garbage Collector use?โ
- **A) 2 (New, Old)
** - **B) 3 (0, 1, 2)
** - **C) 5 levels
** - **D) Infinite
**
๐ก Explanationโ
3 Generations (0, 1, 2).
New objects start in Gen 0. If they survive a cleanup, they are promoted to Gen 1, and eventually Gen 2.
ArchitectInterning
What is the range for "Small Integer Caching" (Interning)?โ
- **A) 0 to 100
** - **B) -1 to 255
** - **C) -5 to 256
** - **D) Any integer under 1000
**
๐ก Explanationโ
-5 to 256.
These integers are pre-allocated singletons. Every time you use them, you reference the existing object rather than creating a new one.
AdvancedTools
Which tool allows you to see the number of references to an object?โ
- **A)
sys.getrefcount(obj)``** - **B)
gc.count(obj)``** - **C)
inspect.refs(obj)``** - **D)
id(obj)``**
๐ก Explanationโ
sys.getrefcount(obj).
This debugging tool shows how many variables are currently pointing to a specific object.
IntermediateMemory Address
What does the id(obj) function return?โ
- **A) The value of the object
** - **B) The unique memory address
** - **C) The size in bytes
** - **D) The object type
**
๐ก Explanationโ
The unique memory address.
It's like the physical shelf number in the warehouse. You use it to check if two variables point to the exact same location.
ArchitectLimitations
What is the "Stop-the-World" problem?โ
- **A) Memory filling up completely
** - **B) A micro-pause during Garbage Collection
** - **C) A segmentation fault crash
** - **D) The server shutting down
**
๐ก Explanationโ
A micro-pause.
When the GC runs on Generation 2 (old objects), it may briefly pause the program. This is critical to know for high-frequency trading apps.
IntermediateComparison
What is the difference between == and is?โ
- **A)
==checks values,ischecks memory IDs** - **B)
==checks memory IDs,ischecks values** - **C) They are identical
** - **D)
isis for math only**
๐ก Explanationโ
is checks memory IDs.
a is b returns True only if they point to the same object. a == b checks if the data inside is equivalent.
AdvancedManual Control
How can you force a Garbage Collection cycle manually?โ
- **A)
sys.clean()``** - **B)
delete all``** - **C)
gc.collect()``** - **D)
memory.free()``**
๐ก Explanationโ
gc.collect().
While rarely needed, you can use this in heavy scripts to force the "Cleaning Crew" to run immediately.
BeginnerDeletion
What does the command del x do?โ
- **A) It destroys the object in Heap immediately
** - **B) It removes the name 'x' and decrements ref count
** - **C) It frees Stack memory only
** - **D) It stops the program
**
๐ก Explanationโ
Removes the name and decrements count.
You never delete objects manually. You just remove the label. If the count hits zero, the GC handles the deletion.
ArchitectOverhead
Why does a Python integer take 28 bytes vs 4 bytes in C?โ
- **A) It stores the number twice
** - **B) Python uses inefficient math
** - **C) It has a "Header" (Type, Ref Count, etc.)
** - **D) It reserves space for future growth
**
๐ก Explanationโ
The Object Header.
Every Python object carries metadata (type, reference count) which adds significant overhead compared to raw C data types.
BeginnerSafety
What common C/C++ error does Python prevent?โ
- **A) Segmentation Faults
** - **B) Syntax Errors
** - **C) Logic Errors
** - **D) Infinite Loops
**
๐ก Explanationโ
Segmentation Faults.
Because you don't manually manage memory (malloc/free), you avoid accessing invalid memory addresses, making Python safer.
AdvancedData Location
Where do objects like lists and functions live?โ
- **A) In the CPU Cache
** - **B) On the private Heap
** - **C) On the Stack
** - **D) On the Hard Drive
**
๐ก Explanationโ
On the private Heap.
All actual data objects live in the Heap. The Stack only contains the references (names) pointing to them.
ArchitectMemory Leaks
If a script eats RAM over 2 days, what is the likely cause?โ
- **A) Too many print statements
** - **B) Stack Overflow
** - **C) A Reference Cycle (Leak)
** - **D) The hard drive is full
**
๐ก Explanationโ
A Reference Cycle.
Complex structures referring to each other can fool the reference counter. If the Generational GC doesn't run or clean them, RAM usage grows.
AdvancedStack Allocation
What lives on the "Stack"?โ
- **A) References (Variable Names)
** - **B) Large data blobs
** - **C) Global constants
** - **D) The Garbage Collector
**
๐ก Explanationโ
References (Variable Names).
The Stack is fast and structured, used for managing function calls and variable names (pointers), while the heavy data sits in the Heap.
BeginnerManagement
Does Python require malloc or free?โ
- **A) Yes, for large lists
** - **B) No, it is Automatic
** - **C) Only in Python 2
** - **D) Yes, for OS interaction
**
๐ก Explanationโ
No, it is Automatic.
The CPython Memory Manager handles all allocation and deallocation behind the scenes.
ArchitectOptimization
Why is understanding id() important for optimization?โ
- **A) It changes the value
** - **B) To check if you are copying data needlessly
** - **C) It forces Garbage Collection
** - **D) It increases memory speed
**
๐ก Explanationโ
To check for needless copying.
If two variables have different IDs when they should be sharing data, you might be wasting RAM by duplicating objects.
๐ Quiz Complete!โ
Your Final Score:
0/20 Novice
Keep practicing to reach Architect level!
- **โบ Retake Quiz
**