Interview questions & answers

Java interview questions & answers

Java is a statically typed, object-oriented programming language that compiles to platform-independent bytecode run by the Java Virtual Machine (JVM). Its write-once-run-anywhere model, automatic garbage collection, and large standard library make it a backbone of enterprise, Android, and backend development. Interviews focus on OOP principles, the JVM, collections, and concurrency.

Updated 2026-06-18 · 15 real, commonly-asked questions with answers.

Key takeaways

  • Java is a statically typed, object-oriented programming language that compiles to platform-independent bytecode run by the Java Virtual Machine (JVM).
  • Core areas to revise for Java: OOP principles, Collections Framework, Exceptions, JVM & garbage collection, Concurrency & threads.
  • This guide answers 15 of the most-asked Java interview questions — rehearse them in OnJob's free AI mock interview.
OOP principlesCollections FrameworkExceptionsJVM & garbage collectionConcurrency & threadsGenericsString handlingStatic vs instance members

Top 15 Java interview questions

Q1.What are the four pillars of OOP in Java?

They are encapsulation (bundling data with the methods that operate on it and hiding internals), inheritance (deriving a class from another to reuse behavior), polymorphism (one interface taking many forms via overriding and overloading), and abstraction (exposing essential behavior while hiding implementation). Java supports all four through classes, interfaces, and access modifiers.

Q2.What is the difference between an abstract class and an interface?

An abstract class can have both abstract and concrete methods, instance fields, and constructors, and a class can extend only one. An interface declares a contract and, since Java 8, can include default and static methods, but its fields are implicitly constant, and a class can implement many interfaces. Use an abstract class for shared state and an interface for capability contracts.

Q3.What is the difference between == and equals() in Java?

The == operator compares references for objects, checking whether two variables point to the same memory location, and compares values for primitives. The equals() method compares logical equality and can be overridden, as String does to compare content. Always override hashCode() whenever you override equals().

Q4.How does garbage collection work in Java?

The JVM automatically reclaims memory occupied by objects that are no longer reachable from any live reference. Modern collectors use generational collection, separating short-lived young-generation objects from long-lived old-generation ones to collect efficiently. Developers cannot force collection; calling System.gc() is only a hint.

Q5.What is the difference between String, StringBuilder, and StringBuffer?

String is immutable, so every modification creates a new object, which is wasteful in loops. StringBuilder is mutable and not thread-safe, making it fast for single-threaded string building. StringBuffer is also mutable but synchronized, so it is thread-safe at the cost of performance.

Q6.Explain the Java Collections Framework hierarchy.

The framework is built on core interfaces: Collection branches into List (ordered, ArrayList/LinkedList), Set (no duplicates, HashSet/TreeSet), and Queue/Deque. Map is a separate hierarchy keyed by unique keys (HashMap/TreeMap/LinkedHashMap). Each implementation offers different ordering, performance, and thread-safety trade-offs.

Q7.What is the difference between ArrayList and LinkedList?

ArrayList uses a dynamic array giving O(1) random access but O(n) insertion or deletion in the middle due to shifting. LinkedList uses a doubly linked list giving O(1) insertion or removal at known positions but O(n) random access. Choose ArrayList for frequent reads and LinkedList for frequent middle insertions.

Q8.What is the difference between HashMap and ConcurrentHashMap?

HashMap is not thread-safe and can corrupt or loop infinitely under concurrent modification. ConcurrentHashMap allows safe concurrent access by locking only segments or buckets rather than the whole map, giving better throughput than a fully synchronized map. HashMap also permits one null key whereas ConcurrentHashMap does not.

Q9.What are checked and unchecked exceptions?

Checked exceptions extend Exception but not RuntimeException and must be either caught or declared with throws, such as IOException. Unchecked exceptions extend RuntimeException and represent programming errors like NullPointerException, requiring no declaration. Errors like OutOfMemoryError are unrecoverable and should not be caught.

Q10.What does the final keyword do?

A final variable cannot be reassigned after initialization, making it effectively constant. A final method cannot be overridden by subclasses, and a final class cannot be extended. It signals intent and lets the compiler and JVM apply certain optimizations.

Q11.What is the difference between static and instance members?

Static members belong to the class itself and are shared across all instances, accessed without creating an object. Instance members belong to each individual object and are created fresh per instantiation. Static methods cannot directly access instance variables because no specific object context exists.

Q12.Explain the difference between throw and throws.

throw is a statement used to actually raise an exception object at runtime. throws is a clause in a method signature that declares which checked exceptions the method might propagate. One triggers an exception; the other documents that one may occur.

Q13.What is the purpose of the JVM, JRE, and JDK?

The JVM (Java Virtual Machine) executes bytecode and provides platform independence. The JRE (Java Runtime Environment) bundles the JVM plus core libraries needed to run programs. The JDK (Java Development Kit) includes the JRE plus development tools like the compiler and debugger needed to build programs.

Q14.What is synchronization and why is it needed?

Synchronization controls access to shared mutable state so only one thread enters a critical section at a time, preventing race conditions. In Java it is achieved with the synchronized keyword on methods or blocks, or with locks from the java.util.concurrent package. Without it, concurrent reads and writes can leave data in an inconsistent state.

Q15.What are generics and why are they useful?

Generics let you parameterize classes and methods with types, such as a List of String, providing compile-time type safety. They eliminate explicit casts and catch type errors at compile time instead of runtime. Due to type erasure, generic type information is removed at runtime, so the JVM sees raw types.

Free AI mock interview

Practise Java out loud

Reading answers is step one. Rehearse them in OnJob's free AI mock interview, get instant feedback, then apply to AI-matched jobs in one click.

Create my free profile — free