Java JVM Memory Model (Heap, Stack, GC) Quiz
Understand the JVM memory architecture including heap and stack management, garbage collection mechanisms, classloader hierarchy, memory leak prevention, and JVM optimization techniques.
Question 1
What is the primary difference between stack and heap memory in JVM?
Question 2
Which memory area is used for storing method parameters and local variables?
Question 3
What happens when the stack memory is full?
Question 4
Which of these is stored in heap memory?
String str = new String("Hello");
int[] array = new int[10];Question 5
What is garbage collection in JVM?
Question 6
Which garbage collection algorithm marks objects and then sweeps unreachable ones?
Question 7
What is the purpose of generational garbage collection?
Question 8
Which JVM memory area stores class-level information?
Question 9
What causes a memory leak in Java?
Question 10
How can static collections cause memory leaks?
public class Cache {
private static Map<String, Object> cache = new HashMap<>();
public static void add(String key, Object value) {
cache.put(key, value);
}
}Question 11
What is the classloader hierarchy in JVM?
Question 12
What is the delegation model in classloading?
Question 13
Which classloader loads the JDK internal classes?
Question 14
What is Metaspace in JVM?
Question 15
How does JVM handle memory allocation for objects?
Question 16
What is a minor GC vs major GC?
Question 17
Which JVM flag sets the maximum heap size?
-Xmx2gQuestion 18
What is JVM heap fragmentation?
Question 19
How can you monitor JVM memory usage?
Question 20
What is the purpose of the -XX:+UseG1GC flag?
java -XX:+UseG1GC -Xmx4g MyAppQuestion 21
What causes ClassNotFoundException?
Question 22
How does JVM optimize memory usage?
Question 23
What is the difference between PermGen and Metaspace?
Question 24
How can listeners cause memory leaks?
public class EventSource {
private List<EventListener> listeners = new ArrayList<>();
public void addListener(EventListener listener) {
listeners.add(listener);
}
}Question 25
What is the JVM's native method stack?
Question 26
How does the JVM handle finalization?
Question 27
What is JVM heap tuning?
Question 28
Which causes OutOfMemoryError: Java heap space?
Question 29
How can you force garbage collection?
System.gc(); // Not recommendedQuestion 30
What is the purpose of the -XX:MaxMetaspaceSize flag?
java -XX:MaxMetaspaceSize=256m MyAppQuestion 31
What is object reachability in garbage collection?
Question 32
How does the JVM handle circular references?
Question 33
What is the JVM's program counter register?
Question 34
How can you detect memory leaks in Java applications?
Question 35
What is the difference between soft, weak, and phantom references?
Question 36
How does JVM optimize string memory usage?
String s1 = "hello"; // String pool
String s2 = new String("hello"); // HeapQuestion 37
What is JVM bytecode verification?
Question 38
How can you reduce GC pauses?
Question 39
What is the JVM's just-in-time (JIT) compilation?
Question 40
Understanding JVM Memory Model means you now write code that:
