Last Updated: December 6, 2025
31 quizzes
Ensure a shared boolean flag write in one thread is visible to other threads immediately
volatile boolean running = ;Click an option to fill the blank:
Safely publish a computed value to other threads using an atomic variable
AtomicInteger counter = new AtomicInteger();Click an option to fill the blank:
Create a new object so it is allocated on the heap and referenced by a local variable
Person person = Person("Alice", 30);Click an option to fill the blank:
In the Java Memory Model, which aspect describes when writes by one thread become observable to another?
Which Java keyword directly participates in defining happens-before relationships for mutual exclusion?
What is stored on the stack during a method call?
Which statement about heap memory in Java is correct?
When does the Java garbage collector mark an object as eligible for collection?
Which generation is primarily targeted by minor garbage collection events in a generational collector?
A common cause of memory leaks in Java applications is:
Which reference type allows an object to be reclaimed as soon as it is only referenced by that type, even if memory is not low?
Which statement about phantom references is correct?
Which tool is typically used to inspect heap dumps and identify memory leaks in Java?
Which JVM flag group enables detailed garbage collection logging on modern JVMs?
Order the steps to fix a visibility problem where a worker thread never stops because it does not see the latest value of a shared flag
Drag and drop to reorder, or use the arrows.
Order the steps performed by a typical mark-and-sweep garbage collector during a collection cycle
Drag and drop to reorder, or use the arrows.
What is the output of this code related to heap vs stack allocation?
1class Counter {
2 int value;
3}
4
5public class HeapStackDemo {
6 public static void main(String[] args) {
7 Counter c1 = new Counter();
8 Counter c2 = c1;
9 c1.value = 5;
10 c2.value = 10;
11 System.out.println(c1.value);
12 }
13}What is the output illustrating happens-before with synchronized blocks?
1public class HappensBeforeDemo {
2 private static int value = 0;
3
4 public static void main(String[] args) throws Exception {
5 Object lock = new Object();
6 Thread writer = new Thread(() -> {
7 synchronized (lock) {
8 value = 42;
9 }
10 });
11 Thread reader = new Thread(() -> {
12 synchronized (lock) {
13 System.out.println(value);
14 }
15 });
16 writer.start();
17 writer.join();
18 reader.start();
19 reader.join();
20 }
21}What is the output showing that local primitives are on the stack and independent from heap objects?
1class Box {
2 int value;
3}
4
5public class StackIndependence {
6 public static void main(String[] args) {
7 Box box = new Box();
8 box.value = 3;
9 int local = box.value;
10 box.value = 7;
11 System.out.println(local);
12 }
13}What is the output of this code demonstrating that unreachable objects are collected logically, not immediately?
1public class ReachabilityDemo {
2 static class Node {
3 int id;
4 Node(int id) { this.id = id; }
5 }
6
7 public static void main(String[] args) {
8 Node node = new Node(1);
9 node = new Node(2);
10 System.out.println(node.id);
11 }
12}What is the output regarding weak references after clearing the strong reference (assuming GC may clear it)?
1import java.lang.ref.WeakReference;
2
3public class WeakRefDemo {
4 public static void main(String[] args) {
5 Object strong = new Object();
6 WeakReference<Object> weak = new WeakReference<>(strong);
7 strong = null;
8 Object ref = weak.get();
9 System.out.println(ref != null);
10 }
11}Identify the bug causing a memory leak risk when using a listener in a long-lived manager
Click on the line(s) that contain the bug.
public class ListenerManager { private static java.util.List<Runnable> listeners = new java.util.ArrayList<>(); public void register(Runnable listener) { listeners.add(listener); } public void clearAll() { // TODO: later }}Find the bug in this code that intends to stop a worker thread using a shared flag
Click on the line(s) that contain the bug.
public class StopWorker { private static boolean running = true; public static void main(String[] args) throws Exception { Thread t = new Thread(() -> { while (running) { // do work } }); t.start(); Thread.sleep(100); running = false; }}Match each memory-related concept with its correct description
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each Java reference type with its typical use case
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code so a background thread periodically checks a volatile flag and stops when requested
public class VolatileFlagDemo { private static volatile boolean = true; public static void main(String[] args) throws InterruptedException { Thread worker = new Thread(() -> { while () { // simulate work } }); worker.start(); Thread.sleep(100); = false; }}Click an option to fill blank 1:
Complete the code to allocate an object on the heap and store its reference in a local variable
public class HeapExample { static class Person { String name; Person(String name) { this.name = name; } } public static void main(String[] args) { p = Person("Alice"); System.out.println(p.name); }}Click an option to fill blank 1:
Complete the code so a map used as a cache does not prevent keys from being garbage collected
import java.util.Map;import java.util.WeakHashMap;public class CacheDemo { private final Map<Object, String> cache = new <>(); public void put(Object key, String value) { cache.put(key, value); } public String get(Object key) { return cache.(key); }}Click an option to fill blank 1:
Complete the code to create a soft reference used for a memory-sensitive cache entry
import java.lang.ref.;public class SoftCacheEntry { private final SoftReference<byte[]> dataRef; public SoftCacheEntry(byte[] data) { this.dataRef = new SoftReference<>(); } public byte[] get() { return dataRef.get(); }}Click an option to fill blank 1:
Click the line that is most likely to cause a memory leak by holding references too long
Click on the line to select.
import java.util.ArrayList;import java.util.List; public class LeakDemo { private static final List<byte[]> cache = new ArrayList<>(); public static void main(String[] args) { for (int i = 0; i < 100; i++) { byte[] data = new byte[1024 * 1024]; cache.add(data); } }}Click the line where the visibility guarantee for the shared flag is established
Click on the line to select.
public class VisibilityFix { private static volatile boolean stopped = false; public static void main(String[] args) { Thread t = new Thread(() -> { while (!stopped) { // work } }); t.start(); }}