Exercises
Explore how Python, Ruby, Java, and C manage memory. This quiz examines stack and heap storage, manual allocation, garbage collection, object reachability, reference counting, weak references, memory leaks, and common hazards such as dangling pointers. Questions compare managed-language behavior with C's explicit memory management and include practical memory diagrams.
Answer the questions below and check the explanation for each answer.
0/13 answered
Auto audio on: the next questions will be read aloud when you click Continue.
free(p) releases the dynamically allocated heap block. The local pointer variable remains in its stack frame until its scope ends, although its stored address becomes invalid after the release.
Java uses reachability analysis. An object becomes eligible when no chain of strong references connects it to a GC root, such as an active local variable, static field, or active thread.
CPython primarily uses reference counting. An ordinary acyclic object can usually be deallocated as soon as its reference count reaches zero, while a separate cyclic collector handles many reference cycles.
malloc allocates storage without initializing it. Its initial contents are indeterminate, so the program must initialize the memory before relying on stored values.
Ruby automatically manages object memory through garbage collection. Unreachable objects can be reclaimed, but programs should not depend on an exact collection time.
Java traces objects from GC roots rather than relying on reference counts. A mutually referencing group is collectible when no object in the group is reachable from a root.
A use-after-free occurs when code accesses an allocation after it has been released. The pointer is dangling, and dereferencing it produces undefined behavior.
A try-with-resources statement automatically invokes close on AutoCloseable resources. Garbage collection manages object memory but should not be relied upon for timely release of external resources.
Each list keeps the other's reference count above zero, so reference counting alone is insufficient. CPython's cyclic garbage collector can identify that the entire cycle is unreachable.
calloc allocates space for an array of elements and initializes all bytes in that storage to zero. In contrast, memory returned by malloc has indeterminate initial contents.
A weak reference permits access to an object without acting as a strong ownership reference. If no strong references remain, the object may be collected and the weak reference will no longer return it.
If realloc fails for a nonzero requested size, it returns NULL and leaves the original allocation unchanged. Using a temporary pointer avoids losing the original address.
Garbage collectors reclaim unreachable objects, not objects that are merely unused. An unintended reference from a cache, collection, listener, or global variable can keep obsolete objects reachable and consume memory.
Video Courses
recommended
Advanced
recommended
Advanced
Advanced
Ideal for beginners
Ideal for beginners
Thousands of online courses in video, ebooks and audiobooks.
To test your knowledge during online courses
Generated directly from your cell phone's photo gallery and sent to your email
Download our app via QR Code or the links below:.
+ 10 million
students
Free and Valid
Certificate
60 thousand free
exercises
4.8/5 rating in
app stores
Free courses in
video and ebooks