Java JVM Memory Model (Heap, Stack, GC) Quiz

Java
0 Passed
0% acceptance

Understand the JVM memory architecture including heap and stack management, garbage collection mechanisms, classloader hierarchy, memory leak prevention, and JVM optimization techniques.

40 Questions
~80 minutes
1

Question 1

What is the primary difference between stack and heap memory in JVM?

A
Stack is for primitive variables and method calls, heap is for objects
B
Stack is faster, heap is slower
C
Stack is managed manually, heap is automatic
D
Stack is larger, heap is smaller
2

Question 2

Which memory area is used for storing method parameters and local variables?

A
Heap memory
B
Stack memory
C
Method area
D
Program counter
3

Question 3

What happens when the stack memory is full?

A
StackOverflowError is thrown
B
OutOfMemoryError is thrown
C
Program terminates gracefully
D
Memory is automatically increased
4

Question 4

Which of these is stored in heap memory?

java
String str = new String("Hello");
int[] array = new int[10];
A
The String object and int array
B
Only the references str and array
C
The literal "Hello" and array elements
D
Nothing, primitives are stored in stack
5

Question 5

What is garbage collection in JVM?

A
Automatic memory management that reclaims unused heap memory
B
Manual memory deallocation by programmer
C
Stack memory cleanup
D
Disk space optimization
6

Question 6

Which garbage collection algorithm marks objects and then sweeps unreachable ones?

A
Mark-and-sweep
B
Copying collector
C
Reference counting
D
Generational GC
7

Question 7

What is the purpose of generational garbage collection?

A
To take advantage of the fact that most objects die young
B
To collect garbage from different memory regions
C
To parallelize garbage collection
D
To compress memory
8

Question 8

Which JVM memory area stores class-level information?

A
Heap
B
Stack
C
Method area (Metaspace)
D
Program counter register
9

Question 9

What causes a memory leak in Java?

A
Objects that are no longer needed but still have references
B
Using too many primitive variables
C
Creating large arrays
D
Using static methods
10

Question 10

How can static collections cause memory leaks?

java
public class Cache {
    private static Map<String, Object> cache = new HashMap<>();
    
    public static void add(String key, Object value) {
        cache.put(key, value);
    }
}
A
Static references prevent garbage collection of the collection and its contents
B
Static methods consume more memory
C
Collections can't be static
D
Only primitive static variables cause leaks
11

Question 11

What is the classloader hierarchy in JVM?

A
Bootstrap -> Extension -> Application (System) ClassLoader
B
Application -> Extension -> Bootstrap ClassLoader
C
System -> Bootstrap -> Extension ClassLoader
D
Extension -> Application -> Bootstrap ClassLoader
12

Question 12

What is the delegation model in classloading?

A
A classloader delegates to its parent before trying to load a class itself
B
Parent classloaders delegate to child classloaders
C
All classloaders work independently
D
Only bootstrap classloader uses delegation
13

Question 13

Which classloader loads the JDK internal classes?

A
Bootstrap ClassLoader
B
Extension ClassLoader
C
Application ClassLoader
D
Custom ClassLoader
14

Question 14

What is Metaspace in JVM?

A
Replacement for PermGen that stores class metadata
B
Area for temporary object storage
C
Stack memory area
D
Garbage collection algorithm
15

Question 15

How does JVM handle memory allocation for objects?

A
Objects are allocated in Eden space of young generation
B
Objects are allocated directly in old generation
C
Objects are allocated in stack memory
D
JVM uses malloc() for allocation
16

Question 16

What is a minor GC vs major GC?

A
Minor GC collects young generation, major GC collects old generation
B
Minor GC is faster, major GC is slower
C
Minor GC runs more frequently, major GC less frequently
D
All of the above
17

Question 17

Which JVM flag sets the maximum heap size?

java
-Xmx2g
A
-Xmx (maximum heap size)
B
-Xms (minimum heap size)
C
-Xmn (young generation size)
D
-Xss (stack size)
18

Question 18

What is JVM heap fragmentation?

A
Free memory scattered in small chunks, making allocation difficult
B
Heap memory becoming full
C
Stack memory overflow
D
Memory leaks accumulation
19

Question 19

How can you monitor JVM memory usage?

A
Using jstat, jmap, VisualVM, or JMX
B
Using System.gc() calls
C
Using Runtime.getRuntime().totalMemory()
D
Only through heap dumps
20

Question 20

What is the purpose of the -XX:+UseG1GC flag?

java
java -XX:+UseG1GC -Xmx4g MyApp
A
Enables the G1 garbage collector
B
Enables parallel GC
C
Enables CMS GC
D
Enables serial GC
21

Question 21

What causes ClassNotFoundException?

A
Class not found in classpath during runtime
B
Class file corrupted
C
Class has compilation errors
D
Class is abstract
22

Question 22

How does JVM optimize memory usage?

A
Through escape analysis, string interning, and object pooling
B
By using more heap memory
C
By reducing stack size
D
By disabling garbage collection
23

Question 23

What is the difference between PermGen and Metaspace?

A
Metaspace is dynamic and uses native memory, PermGen was fixed-size heap area
B
PermGen is in Java 8+, Metaspace is older
C
Metaspace stores objects, PermGen stores classes
D
They are identical
24

Question 24

How can listeners cause memory leaks?

java
public class EventSource {
    private List<EventListener> listeners = new ArrayList<>();
    
    public void addListener(EventListener listener) {
        listeners.add(listener);
    }
}
A
EventSource holds references to listeners, preventing their garbage collection
B
Listeners consume too much memory
C
Event handling is slow
D
Listeners can't be garbage collected
25

Question 25

What is the JVM's native method stack?

A
Stack for native method execution (JNI)
B
Stack for Java method calls
C
Heap area for native objects
D
Memory for native libraries
26

Question 26

How does the JVM handle finalization?

A
finalize() method is called before garbage collection
B
finalize() guarantees immediate cleanup
C
finalize() runs in a separate thread
D
finalize() is deprecated and should not be used
27

Question 27

What is JVM heap tuning?

A
Adjusting heap size, generation sizes, and GC parameters for optimal performance
B
Increasing heap size indefinitely
C
Disabling garbage collection
D
Using only young generation
28

Question 28

Which causes OutOfMemoryError: Java heap space?

A
Heap is full and GC cannot free enough memory
B
Stack is full
C
Metaspace is full
D
Native memory is exhausted
29

Question 29

How can you force garbage collection?

java
System.gc(); // Not recommended
A
Using System.gc() (but it's only a hint)
B
Using Runtime.gc()
C
GC cannot be forced
D
Using -XX:+DisableExplicitGC
30

Question 30

What is the purpose of the -XX:MaxMetaspaceSize flag?

java
java -XX:MaxMetaspaceSize=256m MyApp
A
Limits the maximum size of Metaspace
B
Sets maximum heap size
C
Limits stack size
D
Sets young generation size
31

Question 31

What is object reachability in garbage collection?

A
An object is reachable if there's a path from GC roots to it
B
An object is reachable if it's in young generation
C
An object is reachable if it's not null
D
An object is reachable if it's in heap
32

Question 32

How does the JVM handle circular references?

A
Garbage collector can detect and collect circular references
B
Circular references prevent garbage collection
C
JVM breaks circular references automatically
D
Circular references cause memory leaks
33

Question 33

What is the JVM's program counter register?

A
Contains address of currently executing JVM instruction
B
Counts program execution time
C
Stores program variables
D
Manages program threads
34

Question 34

How can you detect memory leaks in Java applications?

A
Using heap dumps, profilers, and monitoring tools
B
Using System.out.println() statements
C
Using debugger breakpoints
D
Memory leaks cannot be detected
35

Question 35

What is the difference between soft, weak, and phantom references?

A
Soft: cleared before OOM, weak: cleared anytime, phantom: enqueued before reclamation
B
Soft: memory-sensitive, weak: for caches, phantom: for cleanup
C
All of the above
D
They are identical
36

Question 36

How does JVM optimize string memory usage?

java
String s1 = "hello"; // String pool
String s2 = new String("hello"); // Heap
A
Through string interning and pool
B
By compressing strings automatically
C
By storing all strings in heap
D
By limiting string length
37

Question 37

What is JVM bytecode verification?

A
Process that checks bytecode for validity and security
B
Compilation of Java to bytecode
C
Optimization of bytecode
D
Execution of bytecode
38

Question 38

How can you reduce GC pauses?

A
Use larger heap, tune GC parameters, reduce object allocation
B
Use smaller heap
C
Disable GC
D
Use only serial GC
39

Question 39

What is the JVM's just-in-time (JIT) compilation?

A
Converts frequently executed bytecode to native code at runtime
B
Compiles Java source to bytecode
C
Interprets bytecode line by line
D
Optimizes bytecode before execution
40

Question 40

Understanding JVM Memory Model means you now write code that:

A
Properly manages heap and stack memory, understands garbage collection behavior, prevents memory leaks, configures JVM parameters appropriately, and optimizes memory usage for better performance
B
Uses as much memory as possible
C
Ignores memory management entirely
D
Only uses stack memory

QUIZZES IN Java