Java Interview Questions and Answers

Java interviews are famous for going deep on the collections framework, the JVM and concurrency. Freshers are asked about OOP and core classes; mid-level candidates are asked how HashMap and the garbage collector work internally; senior candidates are asked to reason about thread safety, memory visibility and deadlocks.

Usually asked in backend and full stack interviews, from intern / fresher to senior (5+ years).

Intern / Fresher

  1. Q1Intern / Fresher
    What is the difference between JDK, JRE and JVM in Java?

    The JVM runs Java bytecode, the JRE is the JVM plus the standard libraries needed to run Java programs, and the JDK is the JRE plus the development tools such as the compiler (javac).

  2. Q2Intern / Fresher
    What is the difference between == and equals() in Java?

    == compares primitives by value and objects by reference (whether they are the exact same object), while equals() compares the logical content of two objects when the class overrides it.

  3. Q3Intern / Fresher
    What is the difference between String, StringBuilder and StringBuffer in Java?

    String is immutable, StringBuilder is a mutable and fast but not thread-safe buffer, and StringBuffer is a mutable buffer with synchronized methods that is thread-safe but slower.

  4. Q4Intern / Fresher
    What is the difference between final, finally and finalize in Java?

    final is a keyword that prevents reassigning a variable, overriding a method or extending a class; finally is a block that always runs after try/catch for cleanup; finalize() was a method called by the garbage collector and is now deprecated.

Junior (0-2 years)

  1. Q1Junior (0-2 years)
    What is the difference between an abstract class and an interface in Java?

    An abstract class can have instance fields, constructors and a mix of implemented and abstract methods but supports only single inheritance, while an interface defines a contract that a class can implement multiple times and, since Java 8, can include default methods.

  2. Q2Junior (0-2 years)
    What is the difference between ArrayList and LinkedList in Java?

    ArrayList is backed by a resizable array with O(1) index access and O(n) insertion in the middle, while LinkedList is a doubly linked list with O(n) index access and O(1) insertion once you hold the node; in practice ArrayList is faster for most workloads.

  3. Q3Junior (0-2 years)
    What is the difference between checked and unchecked exceptions in Java?

    Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared with throws, while unchecked exceptions (RuntimeException and Error) do not have to be handled and usually signal programming bugs.

  4. Q4Junior (0-2 years)
    What are the main features of Java 8: lambdas, streams and Optional?

    Java 8 introduced lambda expressions, functional interfaces, the Stream API for declarative collection processing, Optional to represent missing values, default methods in interfaces, method references and the java.time date and time API.

Mid-level (2-5 years)

  1. Q1Mid-level (2-5 years)
    How does HashMap work internally in Java?

    A HashMap stores entries in an array of buckets; it computes the key's hash code to choose a bucket, resolves collisions with a linked list that becomes a red-black tree when a bucket grows large, and resizes when the load factor of 0.75 is exceeded.

  2. Q2Mid-level (2-5 years)
    What is the difference between HashMap, Hashtable and ConcurrentHashMap in Java?

    HashMap is not thread-safe, Hashtable is thread-safe by locking the whole map on every method, and ConcurrentHashMap is thread-safe with much finer-grained locking and lock-free reads, so it scales far better.

  3. Q3Mid-level (2-5 years)
    How does JVM memory management and garbage collection work in Java?

    The JVM divides memory into a per-thread stack, a shared heap (split into young and old generations) and Metaspace; the garbage collector frees heap objects that are no longer reachable, collecting short-lived young objects frequently and old objects less often.

Senior (5+ years)

  1. Q1Senior (5+ years)
    What is the difference between synchronized and volatile in Java, and how do you avoid deadlocks?

    synchronized gives mutual exclusion and memory visibility for a block of code, while volatile only guarantees that reads and writes of one variable are visible to all threads and does not make compound operations such as count++ atomic.