Memory Model — Names, References, refcount & GC — Concept Check
Questions on Memory Model — Names, References, refcount & GC. Every value is a heap object; variables are just names bound to objects. Reference counting frees most objects instantly, the cycle collector handles the rest, and __slots__ shrinks per-instance memory.
Questions in this quiz
8 questions on Memory Model — Names, References, refcount & GC — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have two variables, `x` and `y`, both set to 256. When you check `x is y`, it returns True. What explains this behavior?
- AThe `is` operator is unreliable for integers.
- B`x` and `y` are different objects but have the same value.
- CThe `is` operator checks value equality, not identity.
- DPython caches small integers, so `x` and `y` are the same object.
2.A class `Node` has instances `a` and `b` pointing to each other. After deleting `a` and `b`, what happens?
- AThe objects are immediately freed by reference counting.
- BThe objects are freed when `del` is called.
- CThe objects are freed only when the program exits.
- DThe objects remain in memory due to a reference cycle.
3.You create a list `a = [1, 2, 3]` and bind `b` to it. After `b.append(4)`, what does `a` contain?
- A[1, 2, 3, 4, 4]
- B[1, 2, 3]
- C[4]
- D[1, 2, 3, 4]
4.Why might using `is` to compare two strings with the same content give unexpected results?
- AString interning is inconsistent, making `is` unreliable.
- B`is` compares values, not identities.
- CStrings are always interned, so `is` is reliable.
- DStrings are not interned, so `is` is unreliable.
5.A developer uses `del` on a variable pointing to a large object. What is the immediate effect?
- AThe object is moved to a temporary cache.
- BThe object's reference count is decremented.
- CThe object is immediately freed from memory.
- DThe object is marked for deferred deletion.
6.What is a key benefit of using `__slots__` in a Python class?
- AIncreases execution speed.
- BAllows dynamic attribute addition.
- CEnables automatic garbage collection.
- DReduces memory usage per instance.
7.In CPython, what happens when an object's reference count drops to zero?
- AThe object is marked for later deletion.
- BThe object is moved to a garbage collector queue.
- CThe object is cached for reuse.
- DThe object is immediately freed from memory.
8.Why is `del` not always effective in freeing memory in Python?
- A`del` requires manual garbage collection calls.
- B`del` only works in specific Python versions.
- C`del` only works for small objects.
- D`del` only decrements reference count, doesn't handle cycles.