Java Assertions Quiz – 20 Questions with Answers
20 questions · 15 min · Medium
Test your knowledge of Garbage Collection in Java, including heap memory, eligible objects, garbage collectors, finalize, System.gc(), and automatic memory management.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. To automatically reclaim memory occupied by objects that are no longer reachable
Java's garbage collector automatically identifies objects that are no longer reachable and can reclaim the memory they occupy.
B. Heap
Objects created with new are generally allocated in the JVM heap, which is managed by the garbage collector.
B. When it is no longer reachable from any live reference
An object becomes eligible for garbage collection when it can no longer be reached through references from live objects or other GC roots.
A. System.gc()
System.gc() requests that the JVM perform garbage collection, but it does not guarantee that collection will occur immediately.
B. No
System.gc() is only a request to the JVM. The JVM is not required to perform garbage collection immediately in response.
A. A reference is set to null and no other live reference points to the object
If an object has no remaining path from a GC root, it becomes unreachable and may be reclaimed by the garbage collector.
A. JVM
Garbage collection is a responsibility of the Java Virtual Machine, which manages the runtime heap and garbage collectors.
B. A starting point used by the garbage collector to determine reachability
GC roots include runtime references such as active thread stacks and certain static references. Objects reachable from these roots are considered live.
C. The JVM automatically manages object memory reclamation
Unlike languages that require explicit deallocation, Java uses garbage collection to automatically reclaim memory from unreachable objects.
C. It is deprecated and should not be relied upon for resource management
Object finalization is deprecated in modern Java and should not be relied upon for timely or guaranteed resource cleanup.
B. It is generally considered reachable and not eligible for collection
Objects that remain reachable from GC roots are considered live and are not eligible for garbage collection.
A. Keeping unnecessary objects reachable through long-lived references
Garbage collection cannot reclaim an object that is still reachable. Unnecessary long-lived references can therefore cause memory to remain occupied.
B. To exploit the observation that many objects have short lifetimes
Generational collectors organize objects by age because many newly created objects become unreachable relatively quickly.
A. OutOfMemoryError can occur
If the JVM cannot satisfy a memory allocation request after attempting appropriate memory management, it can throw an OutOfMemoryError.
B. It automatically identifies and reclaims memory from objects that are no longer reachable
Java garbage collection automatically manages heap memory by identifying unreachable objects and reclaiming their memory when the JVM's collector determines it is appropriate.