QuizzesJava Memory Management & Garbage Collection
advanced
java

Java Memory Management & Garbage Collection

Test your knowledge of JVM memory areas, heap generations, garbage collection algorithms, memory leaks, and tuning basics.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Memory Management & Garbage Collection. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What are the main memory areas managed by the JVM?

    • AHeap, Stack, Method Area (Metaspace), PC Registers, and Native Method Stack
    • BStack, Queue, Heap, Cache
    • CYoung, Old, Permanent, and Native
    • DCode, Data, BSS, and Heap
  2. 2.What is the Java Heap divided into?

    • AHot and Cold object spaces
    • BPrimary and Secondary spaces
    • CStack-allocated and Heap-allocated regions
    • DYoung Generation (Eden + Survivor spaces) and Old Generation (Tenured)
  3. 3.What is the difference between a Minor GC and a Major (Full) GC?

    • AMinor GC runs every second; Major GC runs every minute
    • BMinor GC collects the Old Generation; Major GC collects Young Generation
    • CThey are the same — "minor" and "major" are informal names for the same process
    • DMinor GC collects the Young Generation (fast); Major/Full GC collects Old Generation or the entire heap (slow)
  4. 4.What does the garbage collector determine before collecting an object?

    • AWhether the object's finalize() method has been called
    • BWhether the object's memory address is in a specific range
    • CWhether the object was created more than 1 second ago
    • DWhether the object is unreachable — no live thread or static reference can reach it
  5. 5.What is a memory leak in Java?

    • AWhen the JVM runs out of native memory
    • BWhen the GC takes too long to run
    • CWhen objects are no longer needed but are still reachable, preventing the GC from collecting them
    • DWhen System.gc() is not called often enough
  6. 6.What are the four types of references in Java?

    • ALocal, Instance, Static, Final
    • BStrong, Soft, Weak, Phantom
    • CHard, Soft, Weak, Strong
    • DDirect, Indirect, Nullable, Optional
  7. 7.What does this code risk?

    import java.util.*;
    public class EventManager {
        private static final List<Object> listeners = new ArrayList<>();
        public static void register(Object listener) {
            listeners.add(listener);
        }
        // No deregister method
    }
    • ANullPointerException when listeners is null
    • BA memory leak — listeners are added to a static list and never removed, preventing GC
    • CStack overflow from recursive calls
    • DThread safety issues only
  8. 8.What is the G1 (Garbage First) garbage collector?

    • AA GC that only runs when System.gc() is called
    • BA low-pause, region-based GC that divides the heap into equal-sized regions and prioritises collecting regions with the most garbage
    • CA concurrent GC only available for the Old Generation
    • DThe original Java 1.0 garbage collector
  9. 9.What JVM flag sets the maximum heap size?

    • A-Xms
    • B-heap:max
    • C-Xmx
    • D-XX:MaxHeap
  10. 10.What is Metaspace (introduced in Java 8)?

    • AA JVM cache for compiled bytecode
    • BA memory pool for String literals
    • CA new heap region for large objects
    • DThe replacement for PermGen — stores class metadata in native memory, growing dynamically by default
  11. 11.What is stop-the-world (STW) in garbage collection?

    • AThe process of releasing native memory
    • BWhen the GC shuts down the JVM
    • CWhen GC runs during application startup only
    • DA pause during which all application threads are suspended while the GC performs its work
  12. 12.What does this code demonstrate?

    public class Main {
        public static void main(String[] args) {
            for (int i = 0; i < 1_000_000; i++) {
                String s = new String("hello");
                // s goes out of scope immediately
            }
            System.out.println("Done — GC cleaned up unreachable Strings");
        }
    }
    • AStackOverflowError after 1000 iterations
    • BOutOfMemoryError because Strings are never freed
    • CA memory leak — all Strings accumulate
    • DCorrect GC behaviour — each String becomes unreachable at end of loop iteration and is eligible for collection
  13. 13.What is object finalization and why is it discouraged?

    • AThe finalize() method called by GC before collecting — discouraged because timing is unpredictable and it can delay collection
    • BA method called before object creation — discouraged due to performance
    • CExplicit null assignment to free memory — discouraged as unnecessary
    • DThe process of making an object immutable — discouraged due to complexity
  14. 14.What is the String pool (interning)?

    • AA special heap area where the JVM caches String literals, allowing identical literals to share one object
    • BA sorted list of all Strings in the application
    • CThe Old Generation region reserved for Strings
    • DA thread-safe pool of reusable String buffers
  15. 15.What does this code print?

    public class Main {
        public static void main(String[] args) {
            String a = "hello";
            String b = "hello";
            String c = new String("hello");
            System.out.println(a == b);
            System.out.println(a == c);
            System.out.println(a.equals(c));
        }
    }
    • Atrue true true
    • Bfalse false true
    • Ctrue false false
    • Dtrue false true
  16. 16.What is a WeakHashMap useful for?

    • AA map with faster iteration than HashMap
    • BA map that uses less memory than HashMap
    • CA map whose keys are weakly referenced — entries are automatically removed when the key is no longer strongly referenced elsewhere
    • DA thread-safe alternative to HashMap
  17. 17.What does -XX:+PrintGCDetails JVM flag do?

    • ADisables GC for performance testing
    • BPrints detailed GC logs including pause times, heap usage before and after, and GC type
    • CForces GC to run every N milliseconds
    • DEnables a GUI for monitoring GC
  18. 18.What is ZGC (Z Garbage Collector)?

    • AA scalable, low-latency GC with sub-millisecond STW pauses regardless of heap size, available since Java 15 as production-ready
    • BA GC only for embedded Java systems
    • CThe first Java garbage collector from version 1.0
    • DA generational GC replacing G1
  19. 19.What causes OutOfMemoryError: Java heap space?

    • AToo many threads running simultaneously
    • BThe heap cannot accommodate new object allocations — either the heap is too small or there is a memory leak
    • CThe Metaspace is full
    • DThe stack overflowed from deep recursion
  20. 20.What is the purpose of System.gc() and should you call it?

    • AShuts down the GC for a performance-critical section
    • BHints to the JVM to run GC — it is advisory only, the JVM may ignore it, and it is generally discouraged in production code
    • CForces immediate GC — call it regularly to keep memory clean
    • DRuns a full GC and blocks until complete