Skip to content
QuiznExam

Garbage Collection in Java

Test your knowledge of Garbage Collection in Java, including heap memory, eligible objects, garbage collectors, finalize, System.gc(), and automatic memory management.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of garbage collection in Java?

    • A. To automatically reclaim memory occupied by objects that are no longer reachable
    • B. To compile Java source code
    • C. To create new objects
    • D. To manage database connections
    Show answer

    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.

  2. 2. Where are Java objects generally allocated?

    • A. Stack
    • B. Heap
    • C. CPU register only
    • D. Source code area
    Show answer

    B. Heap

    Objects created with new are generally allocated in the JVM heap, which is managed by the garbage collector.

  3. 3. When does an object generally become eligible for garbage collection?

    • A. When the object is created
    • B. When it is no longer reachable from any live reference
    • C. When a method returns an integer
    • D. When the program imports a package
    Show answer

    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.

  4. 4. Which method can be used to request garbage collection?

    • A. System.gc()
    • B. System.collect()
    • C. Runtime.delete()
    • D. Memory.free()
    Show answer

    A. System.gc()

    System.gc() requests that the JVM perform garbage collection, but it does not guarantee that collection will occur immediately.

  5. 5. Does calling System.gc() guarantee that garbage collection will happen immediately?

    • A. Yes, always
    • B. No
    • C. Only inside static methods
    • D. Only when the heap is empty
    Show answer

    B. No

    System.gc() is only a request to the JVM. The JVM is not required to perform garbage collection immediately in response.

  6. 6. Which of the following is an example of an object becoming unreachable?

    • A. A reference is set to null and no other live reference points to the object
    • B. A new reference is created to the object
    • C. The object is stored in a reachable collection
    • D. A method reads a field from the object
    Show answer

    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.

  7. 7. Which component of the Java runtime is responsible for performing garbage collection?

    • A. JVM
    • B. JDK compiler only
    • C. Java source file
    • D. Java package manager
    Show answer

    A. JVM

    Garbage collection is a responsibility of the Java Virtual Machine, which manages the runtime heap and garbage collectors.

  8. 8. What is a GC root in the context of Java garbage collection?

    • A. An object that must always be deleted
    • B. A starting point used by the garbage collector to determine reachability
    • C. The first object created by a program
    • D. A special Java keyword
    Show answer

    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.

  9. 9. Which statement about Java garbage collection is correct?

    • A. Developers normally call free() for every object
    • B. Developers manually release heap memory using delete
    • C. The JVM automatically manages object memory reclamation
    • D. Java never releases unused object memory
    Show answer

    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.

  10. 10. Which statement about the finalize() method is correct in modern Java?

    • A. It guarantees execution before an object is collected
    • B. It is the preferred way to release resources
    • C. It is deprecated and should not be relied upon for resource management
    • D. It forces garbage collection
    Show answer

    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.

  11. 11. What happens to an object that remains strongly reachable from a live GC root?

    • A. It is considered eligible for garbage collection
    • B. It is generally considered reachable and not eligible for collection
    • C. It is automatically deleted
    • D. It is moved to the stack
    Show answer

    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.

  12. 12. Which situation can contribute to a memory leak in a Java application despite having garbage collection?

    • A. Keeping unnecessary objects reachable through long-lived references
    • B. Using local variables
    • C. Calling a normal method
    • D. Creating a primitive int variable
    Show answer

    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.

  13. 13. What is the purpose of generational garbage collection?

    • A. To divide Java source code into packages
    • B. To exploit the observation that many objects have short lifetimes
    • C. To prevent all objects from being created
    • D. To move all objects permanently to the stack
    Show answer

    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.

  14. 14. What can happen when the JVM cannot allocate enough heap memory and garbage collection cannot free sufficient space?

    • A. OutOfMemoryError can occur
    • B. The JVM automatically creates unlimited memory
    • C. The program always continues normally
    • D. All objects are immediately deleted
    Show answer

    A. OutOfMemoryError can occur

    If the JVM cannot satisfy a memory allocation request after attempting appropriate memory management, it can throw an OutOfMemoryError.

  15. 15. Which statement best describes garbage collection in Java?

    • A. It guarantees that all unused memory is immediately returned to the operating system
    • B. It automatically identifies and reclaims memory from objects that are no longer reachable
    • C. It removes all objects when a method ends
    • D. It requires programmers to manually delete every object
    Show answer

    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.