Skip to content
C

Java Interview Questions

JVM Interview Questions

JVM architecture, class loading, runtime data areas, garbage collection, and common memory errors.

Question 1: What is a class loader, and what are its three main responsibilities?

Ans

A class loader is the JVM component responsible for bringing a compiled class into memory when it's needed, through three phases: loading, finding and reading the .class bytecode, linking, verifying the bytecode is valid, preparing static fields with default values, and resolving symbolic references, and initialization, running static initializers and assigning actual values to static fields.

Example

text
.class file -> Loading -> Linking (verify, prepare, resolve) -> Initialization -> ready to use

Important Point

A class is only loaded when it's actually needed, lazy loading, not all at once when the program starts.

Question 2: What is the Heap, and what is stored there?

Ans

The Heap is the shared memory area where every object and array created with new is actually allocated, regardless of which thread or method created it, and it's the memory region the garbage collector manages and cleans up.

Example

java
Student s = new Student(); // the Student object itself lives on the Heap

Important Point

Because the Heap is shared across all threads, concurrent access to the same heap object requires proper synchronization — the Heap itself doesn't provide that automatically.

Question 3: What is the Stack, and what is stored there?

Ans

Each thread gets its own Stack, which stores method call frames, one frame per active method call, holding that method's local variables, its operand stack for intermediate computation, and information about where to return to when the method finishes.

Example

java
void calculate() { int x = 10; // x lives in calculate()'s stack frame, not on the Heap }

Important Point

Because each thread has its own Stack, local variables, unlike shared objects, are naturally thread-safe without needing synchronization.

Question 4: What is the difference between the interpreter and the JIT compiler in the execution engine?

Ans

The interpreter reads and executes bytecode instructions one at a time, which starts up quickly but is relatively slow for code that runs repeatedly. The JIT, Just-In-Time, compiler monitors which methods run frequently, "hot" code, and compiles those specific methods into optimized native machine code at runtime, so later calls run at near-native speed instead of being reinterpreted every time.

Important Point

This combination, interpret first then JIT-compile hot paths, is why long-running Java applications often get noticeably faster the longer they run, as more hot code gets JIT-compiled.

Question 5: What is garbage collection, and what makes an object eligible for it?

Ans

Garbage collection automatically finds Heap objects that are no longer reachable through any live reference chain from active threads or static fields, and reclaims their memory without the programmer manually freeing it. An object becomes eligible once nothing in the program can reach it anymore, for example, after its only reference variable is set to null or goes out of scope.

Example

java
Student s = new Student(); s = null; // the original Student object may now become eligible for garbage collection

Important Point

Becoming eligible for garbage collection doesn't mean the object is collected immediately — the exact timing is controlled by the garbage collector, not the programmer.

Question 6: What is finalize()?

Ans

finalize() was an old mechanism associated with garbage-collection-based cleanup. It is deprecated and should not be used for reliable resource management.

Cleanup of files, sockets, database connections, and similar resources should be explicit or use AutoCloseable with try-with-resources.

Example

java
try (var input = new java.io.FileInputStream("a.txt")) { System.out.println(input.read()); }

Important Point

Do not tell an interviewer that `finalize()` guarantees cleanup; it does not.

Question 7: What is memory leak in Java?

Ans

A Java memory leak occurs when an application unintentionally keeps references to objects that it no longer needs, preventing garbage collection.

Common causes include static collections, unremoved listeners, caches without eviction, and long-lived objects holding unnecessary references.

Example

java
static List<byte[]> cache = new ArrayList<>(); // continually adding unused data can retain memory

Important Point

Java has automatic memory management, but developers still need to manage object reachability and resource lifecycles.

Continue Your Preparation