Java Progressive Master Question Bank
75 progressive Java interview questions complete with technical explanations, key takeaways, diagrams, code examples, and interactive knowledge check quizzes.
Q1: What are JDK, JRE, and JVM, and how do they differ?
- JVM (Java Virtual Machine): The abstract runtime engine that executes compiled Java bytecode (
.classfiles). - JRE (Java Runtime Environment): Includes the JVM plus standard runtime libraries and class loaders required to execute Java programs.
- JDK (Java Development Kit): The complete software development kit containing the JRE, compiler (
javac), debugger (jdb), and development tools.
# 1. Compiler (JDK component) produces bytecode
javac Main.java -> Main.class
# 2. Virtual Machine (JRE/JVM component) executes bytecode
java Main- JVM executes bytecode; JRE = JVM + Standard Libraries; JDK = JRE + Developer Tools (
javac). - JDK is needed for writing and compiling code; JRE is sufficient for execution.
Which software distribution is strictly required on a developer machine to compile .java source files?
Q2: How does Java achieve platform independence?
Java achieves platform independence through the concept of 'Write Once, Run Anywhere' (WORA). The Java compiler (javac) translates source code into an intermediate format called Bytecode (.class). Bytecode is platform-neutral; platform-specific JVM implementations interpret or JIT-compile this bytecode into native machine code.
- Source code compiles to platform-neutral Bytecode (
.class). - JVM implementations are platform-specific, making Java bytecode platform-independent.
Is the JVM itself platform-independent?
Q3: What is the JIT Compiler and how does Tiered Compilation work in HotSpot JVM?
The Just-In-Time (JIT) Compiler is a JVM subsystem that improves runtime performance by compiling frequently executed bytecode sequences ('hot spots') directly into native machine code at runtime. HotSpot uses Tiered Compilation:
- Tier 0: Interpreted bytecode execution.
- Tiers 1-3 (C1 Compiler / Client Compiler): Fast compilation with basic optimizations.
- Tier 4 (C2 Compiler / Server Compiler): Heavy profile-guided optimizations (inlining, escape analysis, loop unrolling).
- JIT translates hot bytecode paths into native machine code at runtime.
- Tiered compilation balances rapid startup time (C1) with high peak performance (C2).
What is the primary role of the C2 compiler in HotSpot Tiered Compilation?
Q4: Why is the main method declared as public static void main(String[] args)?
public: Allows the JVM execution engine (outside the package) to call the method.static: Enables the JVM to invoke the method without instantiating an instance of the outer class.void: Signals that the entry point returns no exit code value (JVM usesSystem.exit()for exit codes).main: The standard identifier searched for by the launcher.String[] args: Receives command-line arguments passed at application invocation.
public class Application {
public static void main(String[] args) {
System.out.println("Application Entry Point");
}
}staticavoids requiring object allocation prior to execution initialization.String[] argscaptures command-line parameters.
What occurs if you omit the static keyword from the main method in Java 8?
Q5: What are Primitive Data Types versus Reference Types in Java?
- Primitive Types (8 total):
byte,short,int,long,float,double,char,boolean. They store raw binary values directly on the stack or inline inside heap objects. - Reference Types: Objects, Arrays, Interfaces, and Enums. They store memory addresses (references) pointing to heap-allocated object instances.
- Primitives hold raw values directly; Reference types hold memory address references to heap objects.
- Primitives cannot be
null; Reference variables default tonull.
Which of the following is NOT a Java primitive data type?
Q6: What are Autoboxing and Unboxing, and what performance pitfalls do they present?
- Autoboxing: Automatic conversion of primitives to wrapper objects (
int->Integer). - Unboxing: Automatic conversion of wrapper objects to primitives (
Integer->int). - Pitfalls: Unboxing a
nullwrapper throwsNullPointerException. Excessive autoboxing inside loops creates huge GC object pressure.
// Bad: Autoboxes 1,000,000 Integer objects
Long sum = 0L;
for (long i = 0; i < 1_000_000; i++) {
sum += i; // Unboxes sum, adds i, autoboxes result to new Long object
}
// Good: Uses primitive long
long fastSum = 0L;
for (long i = 0; i < 1_000_000; i++) {
fastSum += i;
}- Autoboxing creates new object wrapper allocations implicitly.
- Unboxing
nullwrapper references throwsNullPointerException.
What error occurs when executing: Integer val = null; int num = val;
Q7: Why are Java Strings immutable and how does the String Constant Pool work?
Strings in Java are final and immutable for Security (db connections, network parameters), Thread-Safety (shared without synchronization), and Caching.
The String Constant Pool (SCP) is a special heap area. String literals are stored here and shared across the JVM to reduce duplicate memory footprint.
String s1 = "Java"; // Stored in String Pool
String s2 = "Java"; // Reuses pooled instance
String s3 = new String("Java"); // Explicitly creates new Heap object
System.out.println(s1 == s2); // true (same reference)
System.out.println(s1 == s3); // false (different heap memory addresses)- Immutability enables safe sharing, hash code caching, and thread safety.
- String literals are stored in the String Constant Pool inside heap memory.
Where is the String Constant Pool located in modern Java architectures (Java 7+)?
Q8: What are the differences between String, StringBuilder, and StringBuffer?
String: Immutable character sequence. Modifications create new object instances.StringBuilder: Mutable character sequence. Non-thread-safe, high performance.StringBuffer: Mutable character sequence. Thread-safe usingsynchronizedmethods (slower overhead).
- Use
Stringfor immutable constants. - Use
StringBuilderfor single-threaded dynamic string concatenation. - Use
StringBufferfor multi-threaded thread-safe string concatenation.
Which class should be preferred for high-speed string concatenation in single-threaded methods?
Q9: What is the difference between == and the .equals() method?
==Operator: Compares primitive values directly, or checks reference identity (whether two reference variables point to the exact same memory address on the heap)..equals()Method: Compares logical content equality when overridden by a class (e.g.,String,Integer). The default implementation inObjectuses==reference identity.
==compares memory references;.equals()compares logical values (if overridden).- Always override
.hashCode()whenever overriding.equals().
What is the output of: new String("A").equals(new String("A"))?
Q10: What is the equals() and hashCode() contract, and why must both be overridden together?
The contract dictates:
- If
a.equals(b)istrue, thena.hashCode() == b.hashCode()MUST betrue. - If
a.hashCode() == b.hashCode(),a.equals(b)may or may not betrue(hash collision). If you override.equals()without.hashCode(), key lookups in hash-based collections (HashMap,HashSet) will fail because duplicate objects resolve to different bucket indices.
public class Person {
private final int id;
private final String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person person)) return false;
return id == person.id && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}- Equal objects MUST have equal hash codes.
- Failing to override
hashCode()breaksHashMapandHashSetlookups.
What happens if two distinct instances return the exact same hashCode() value?