SCJP Java Garbage Collection and Memory Management — Questions and Answers
Question 1: After the following code runs, how many objects are eligible for garbage collection? String a = new String("hello"); String b = new String("world"); a = b; b = null;
- 0
- 1 (Correct answer)
- 2
- 3
Correct answer: 1
After `a = b`, variable `a` now points to the "world" object and the original "hello" object has no references — it is eligible for GC. After `b = null`, `b` no longer points to "world", but `a` still does, so "world" is NOT eligible. Only 1 object ("hello") is eligible.
Question 2: Which statement about the `finalize()` method is TRUE?
- It is guaranteed to run before the object's memory is reclaimed
- It is called by the garbage collector at most once per object (Correct answer)
- You should always call super.finalize() first and then clean up resources
- It runs on the same thread as the code that created the object
Correct answer: It is called by the garbage collector at most once per object
The JVM guarantees that finalize() is called at most once per object, even if the object becomes reachable again inside finalize() (resurrection). Its execution is NOT guaranteed at all — the GC may never call it, and if it does, the timing is undefined.
Question 3: What is the effect of calling `System.gc()`?
- It immediately frees all unreachable objects
- It is a request to the JVM to run garbage collection; the JVM may ignore it (Correct answer)
- It forces all finalizers to run immediately
- It causes a compile-time error in Java SE 6
Correct answer: It is a request to the JVM to run garbage collection; the JVM may ignore it
`System.gc()` is only a hint to the JVM that now might be a good time to run garbage collection. The JVM is free to ignore this request. It does not guarantee immediate collection or finalization.
Question 4: Which of the following creates an 'island of isolation' (objects eligible for GC even though they reference each other)?
- Two objects that reference each other but have no external references pointing to them (Correct answer)
- A single object whose reference is set to null
- An object stored inside a static field
- An object passed as a method parameter
Correct answer: Two objects that reference each other but have no external references pointing to them
Java's GC uses reachability from GC roots, not reference counting. Two objects referencing each other but unreachable from any GC root form an 'island of isolation' and are both eligible for collection, despite referencing each other.
Question 5: Where are local primitive variables stored in a Java program?
- The heap
- The stack (Correct answer)
- The method area
- The constant pool
Correct answer: The stack
Local variables (both primitive and object references) are stored on the thread's call stack. The objects that reference variables point to are stored on the heap. The method area/constant pool stores class-level data and string literals.
Question 6: Given the following, which objects are eligible for GC after line 5? 1: Object a = new Object(); // Obj-A 2: Object b = new Object(); // Obj-B 3: a = b; 4: b = new Object(); // Obj-C 5: b = null;
- Obj-A only
- Obj-C only
- Obj-A and Obj-C (Correct answer)
- None — all objects still have references
Correct answer: Obj-A and Obj-C
After line 3, `a` = Obj-B, so Obj-A loses all references (eligible). After line 4, `b` = Obj-C, and `a` still holds Obj-B. After line 5, `b` = null so Obj-C loses its only reference (eligible). Obj-B is still referenced by `a` and is NOT eligible. So Obj-A and Obj-C are both eligible for GC.
After the following code runs, how many objects are eligible for garbage collection?
String a = new String("hello");
String b = new String("world");
a = b;
b = null;