← All SCJP Flashcard Decks

Java Garbage Collection and Memory Management Flashcards

6 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 6 Java Garbage Collection and Memory Management flashcards as text
  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;

    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.

  2. Which statement about the `finalize()` method is TRUE?

    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.

  3. What is the effect of calling `System.gc()`?

    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.

  4. Which of the following creates an 'island of isolation' (objects eligible for GC even though they reference each other)?

    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.

  5. Where are local primitive variables stored in a Java program?

    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.

  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;

    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.

How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy