HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
75 Topics
Chapter•390 Minutes

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 (.class files).
  • 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.
Relationship between JDK, JRE, and JVM
Compilation vs Execution Pipeline
# 1. Compiler (JDK component) produces bytecode
javac Main.java -> Main.class

# 2. Virtual Machine (JRE/JVM component) executes bytecode
java Main
💡 javac converts human-readable source code into platform-independent JVM bytecode.
Key Takeaways
  • 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.
Knowledge Check

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.

Key Takeaways
  • Source code compiles to platform-neutral Bytecode (.class).
  • JVM implementations are platform-specific, making Java bytecode platform-independent.
Knowledge Check

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).
Key Takeaways
  • JIT translates hot bytecode paths into native machine code at runtime.
  • Tiered compilation balances rapid startup time (C1) with high peak performance (C2).
Knowledge Check

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 uses System.exit() for exit codes).
  • main: The standard identifier searched for by the launcher.
  • String[] args: Receives command-line arguments passed at application invocation.
Standard Main Method Signature
public class Application {
    public static void main(String[] args) {
        System.out.println("Application Entry Point");
    }
}
💡 The public static signature allows JVM launchers to call the entry point directly.
Key Takeaways
  • static avoids requiring object allocation prior to execution initialization.
  • String[] args captures command-line parameters.
Knowledge Check

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.
Key Takeaways
  • Primitives hold raw values directly; Reference types hold memory address references to heap objects.
  • Primitives cannot be null; Reference variables default to null.
Knowledge Check

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 null wrapper throws NullPointerException. Excessive autoboxing inside loops creates huge GC object pressure.
Autoboxing Performance Leak
// 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;
}
💡 Using wrapper objects in accumulation loops allocates millions of redundant heap wrapper instances.
Key Takeaways
  • Autoboxing creates new object wrapper allocations implicitly.
  • Unboxing null wrapper references throws NullPointerException.
Knowledge Check

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 Literal vs New Instance Allocation
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)
💡 Literals reuse SCP instances; string construction via new allocates distinct heap instances.
Key Takeaways
  • Immutability enables safe sharing, hash code caching, and thread safety.
  • String literals are stored in the String Constant Pool inside heap memory.
Knowledge Check

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 using synchronized methods (slower overhead).
Key Takeaways
  • Use String for immutable constants.
  • Use StringBuilder for single-threaded dynamic string concatenation.
  • Use StringBuffer for multi-threaded thread-safe string concatenation.
Knowledge Check

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 in Object uses == reference identity.
Key Takeaways
  • == compares memory references; .equals() compares logical values (if overridden).
  • Always override .hashCode() whenever overriding .equals().
Knowledge Check

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:

  1. If a.equals(b) is true, then a.hashCode() == b.hashCode() MUST be true.
  2. If a.hashCode() == b.hashCode(), a.equals(b) may or may not be true (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.
Overriding equals and hashCode
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);
    }
}
💡 Both methods evaluate the exact same identity fields to satisfy the Java collection contract.
Key Takeaways
  • Equal objects MUST have equal hash codes.
  • Failing to override hashCode() breaks HashMap and HashSet lookups.
Knowledge Check

What happens if two distinct instances return the exact same hashCode() value?