JVM, JRE & JDK Chapter
JVM Interview Questions and Answers
Learn how the Java Virtual Machine executes bytecode, from the Class Loader and Execution Engine to JIT compilation and Garbage Collection.
History of Java interview questions
JVM Interview Question 10 Questions
Click on any question to expand the answer.
Interview Answer
The Java Virtual Machine (JVM) is a virtual runtime environment that executes Java bytecode. It acts as a bridge between Java programs and the underlying operating system by converting bytecode into native machine code. The JVM is responsible for memory management, garbage collection, security, exception handling, and platform independence.
Key Points
- JVM executes Java bytecode (.class files)
- Provides platform independence
- Converts bytecode into machine code
- Manages memory and garbage collection
- Handles exception management and security
- Forms the core of the Java runtime
Example
Hello.java
|
v
javac
|
v
Hello.class
|
v
JVM
|
v
Machine Code
|
v
Program OutputInterview Tips
- Do not say the JVM compiles Java source code; it executes bytecode.
- Mention memory management and garbage collection.
- Explain that every operating system has its own JVM implementation.
Summary
The JVM is the engine that runs Java applications by executing bytecode. It provides platform independence, automatic memory management, security, and efficient program execution.
Interview Answer
The JVM is responsible for executing Java bytecode. The JRE includes the JVM along with libraries required to run Java applications, while the JDK contains the JRE plus development tools such as the Java compiler and debugger. Developers use the JDK to build applications, whereas end users only need the JRE to run them.
Key Points
- JVM executes bytecode
- JRE = JVM + runtime libraries
- JDK = JRE + development tools
- javac is included in the JDK
- Developers install the JDK
- The JDK is a superset of the JRE
Example
JDK
├── JRE
│ ├── JVM
│ └── Runtime Libraries
├── javac
├── javadoc
├── jar
└── jdbInterview Tips
- Remember the hierarchy: JDK → JRE → JVM.
- Explain the purpose of each component.
- Mention that modern JDK releases no longer ship a separate standalone JRE.
Summary
The JVM executes programs, the JRE provides the runtime environment, and the JDK provides everything needed for Java development. Understanding their relationship is a common interview topic.
Interview Answer
A Java source file is first compiled by the javac compiler into bytecode stored in a .class file. The JVM loads this bytecode, verifies it for security, and executes it using the Interpreter and the Just-In-Time (JIT) Compiler. Frequently executed code is compiled into native machine code to improve performance.
Key Points
- Source code is compiled into bytecode
- Class Loader loads the bytecode
- Bytecode Verifier checks security
- Interpreter executes bytecode
- JIT Compiler optimizes frequently used code
- CPU executes native machine code
Example
Hello.java
|
v
javac
|
v
Hello.class
|
v
Class Loader
|
v
Bytecode Verifier
|
v
Interpreter + JIT Compiler
|
v
Machine Code
|
v
CPU Executes ProgramInterview Tips
- Explain that Java is both compiled and interpreted.
- Mention the role of the JIT Compiler.
- Describe bytecode verification before execution.
Summary
The JVM converts Java bytecode into machine code through the Interpreter and JIT Compiler. This process enables secure, optimized, and platform-independent execution.
Interview Answer
The JVM consists of several components that work together to execute Java applications efficiently. These include the Class Loader, Runtime Data Areas, Execution Engine, Native Method Interface (JNI), and Native Method Libraries. Each component has a specific responsibility during program execution.
Key Points
- Class Loader loads class files
- Runtime Data Areas store program data
- Execution Engine executes bytecode
- JNI allows interaction with native code
- Native Libraries provide platform-specific functionality
- Components work together for efficient execution
Example
Java Program
|
v
Class Loader
|
v
Runtime Data Areas
|
v
Execution Engine
|
+------> JNI ------> Native Libraries
|
v
Program OutputInterview Tips
- Remember all major JVM components.
- Explain the purpose of each instead of only listing them.
- Mention that Runtime Data Areas include Heap, Stack, Method Area, PC Register, and Native Method Stack.
Summary
The JVM architecture includes multiple components that load classes, manage memory, execute bytecode, and interact with native libraries. Together they provide secure and efficient execution.
Interview Answer
The Class Loader is a JVM subsystem responsible for loading Java class files into memory only when they are needed. It follows the parent delegation model to prevent duplicate loading and improve security. After loading a class, the JVM links and initializes it before execution.
Key Points
- Loads .class files dynamically
- Uses lazy loading
- Follows the parent delegation model
- Performs Loading, Linking, and Initialization
- Prevents duplicate class loading
- Improves security and performance
Example
Application Starts
|
v
Bootstrap Class Loader
|
v
Platform Class Loader
|
v
Application Class Loader
|
v
Loading
|
v
Linking
|
v
Initialization
|
v
Class Ready for ExecutionInterview Tips
- Remember the three phases: Loading, Linking, and Initialization.
- Explain the parent delegation model clearly.
- Mention the three built-in class loaders: Bootstrap, Platform, and Application.
Summary
The Class Loader dynamically loads Java classes into memory and prepares them for execution. Its delegation model improves security, avoids duplicate loading, and ensures efficient class management.
Interview Answer
The JVM divides memory into different runtime areas to manage program execution efficiently. Each area stores specific types of data, such as objects, method information, local variables, and execution details. Some memory areas are shared among threads, while others are thread-specific.
Key Points
- Heap stores objects and instance variables
- Method Area stores class metadata, static variables, and runtime constant pool
- Java Stack stores local variables, method calls, and partial results
- PC Register stores the address of the current instruction for each thread
- Native Method Stack stores data for native method execution
- Heap and Method Area are shared; Stack, PC Register, and Native Method Stack are thread-specific
Example
JVM Runtime Memory
Method Area
|
Heap
|
Thread 1
├── Java Stack
├── PC Register
└── Native Method Stack
Thread 2
├── Java Stack
├── PC Register
└── Native Method StackInterview Tips
- Remember which memory areas are shared and which are thread-specific.
- Heap stores objects, while Stack stores method execution data.
- Explain the purpose of each memory area instead of only listing them.
Summary
The JVM runtime memory is divided into multiple areas, each with a specific responsibility. This organization enables efficient execution, memory management, and multithreading.
Interview Answer
The Execution Engine is the JVM component responsible for executing Java bytecode. It uses the Interpreter to execute bytecode line by line, while the JIT Compiler converts frequently executed code into native machine code for better performance. The Garbage Collector automatically removes unused objects to free memory during execution.
Key Points
- Execution Engine executes bytecode
- Interpreter executes bytecode instruction by instruction
- JIT Compiler converts hot code into native machine code
- Garbage Collector reclaims unused memory
- JIT improves execution speed
- All three components work together for efficient execution
Example
Bytecode
|
v
Execution Engine
|
+--> Interpreter
|
+--> JIT Compiler
|
+--> Garbage Collector
|
v
Program OutputInterview Tips
- Explain the role of each component separately.
- Mention that JIT compiles only frequently executed code.
- Garbage Collection improves memory management automatically.
Summary
The Execution Engine combines interpretation, compilation, and garbage collection to execute Java applications efficiently. This combination provides both portability and high performance.
Interview Answer
Just-In-Time (JIT) Compilation is a JVM optimization technique that converts frequently executed bytecode into native machine code during runtime. Instead of interpreting the same instructions repeatedly, the JVM executes the compiled machine code directly. This significantly improves application performance.
Key Points
- JIT is part of the Execution Engine
- Converts bytecode into machine code at runtime
- Optimizes frequently executed methods
- Reduces repeated interpretation
- Improves execution speed
- Maintains platform independence
Example
Bytecode
|
Interpreter
|
Frequently Executed Code
|
JIT Compiler
|
Machine Code
|
CPU Executes FasterInterview Tips
- Mention that JIT works during runtime, not compile time.
- Explain that only hot methods are compiled.
- Differentiate JIT from the javac compiler.
Summary
JIT Compilation improves Java performance by converting frequently used bytecode into native machine code. It is one of the main reasons modern Java applications are highly efficient.
Interview Answer
Garbage Collection (GC) is the JVM process that automatically removes objects that are no longer referenced by the application. It frees heap memory so that developers do not need to manually deallocate memory. Modern JVMs provide multiple garbage collectors optimized for different performance requirements.
Key Points
- GC automatically frees unused heap memory
- Eliminates manual memory management
- Prevents most memory leaks caused by unused objects
- Runs automatically when required
- Common collectors include Serial GC, Parallel GC, G1 GC, ZGC, and Shenandoah GC
- Different collectors focus on throughput, latency, or scalability
Example
Object Created
|
Object Used
|
Reference Removed
|
Garbage Collector Detects Object
|
Memory ReclaimedInterview Tips
- GC works only on heap memory.
- Objects with active references are not collected.
- Know the popular garbage collectors and their primary use cases.
Summary
Garbage Collection automatically manages heap memory by removing unreachable objects. Different garbage collectors are available to balance application throughput, pause time, and scalability.
Interview Answer
The JVM provides platform independence by executing platform-neutral bytecode on operating system-specific JVM implementations. It ensures security through bytecode verification and class loading, manages memory using automatic garbage collection, and improves performance with the JIT Compiler. When a Java application starts, the JVM loads classes, initializes them, allocates memory, and begins execution; when the application terminates, the JVM completes remaining tasks and releases allocated resources.
Key Points
- Bytecode enables platform independence
- Bytecode Verifier improves security
- Class Loader loads required classes
- Garbage Collector manages memory automatically
- JIT Compiler improves execution speed
- JVM initializes, executes, and shuts down applications systematically
Example
Application Starts
|
v
JVM Starts
|
v
Class Loading
|
v
Memory Allocation
|
v
Bytecode Verification
|
v
Execution Engine
|
v
Program Execution
|
v
Garbage Collection
|
v
Application Terminates
|
v
Resources ReleasedInterview Tips
- Explain the complete JVM lifecycle step by step.
- Mention all four features: platform independence, security, memory management, and performance.
- Relate the lifecycle to actual program execution from start to finish.
Summary
The JVM is responsible for the complete lifecycle of a Java application, from startup to termination. Its architecture combines portability, security, automatic memory management, and runtime optimization to execute Java applications efficiently across different platforms.