Java Interview Questions and Answers
Java interviews test three things: how well you know the language (OOP, collections, generics, exceptions, streams), how you reason about concurrency and memory (threads, locks, the Java memory model, garbage collection), and how you apply it to real services (Spring, databases, performance). The questions below cover all three, each with a worked answer and code.
Start with the fundamentals, then move to concurrency and JVM internals, which is where mid-level and senior interviews spend most of their time. Mark what you have read, revise what you missed, and use the coding problems to practise the same ideas under a timer.
Questions
- What are the four fundamental OOP principles?
- Abstract class vs interface in Java?
- What is the String pool in Java?
- Explain method overloading vs overriding.
- What is the difference between `==` and `.equals()`?
- How does Java achieve platform independence?
- What are access modifiers and their scopes?
- Explain constructor chaining.
- What is the `final` keyword used for?
- What is `this` and `super`?
- What is immutability and how to create an immutable class?
- Difference between checked and unchecked exceptions?
- What is the `try-with-resources` statement?
- Explain the `Exception` hierarchy in Java.
- What is method hiding vs method overriding?
- What is a singleton class and how to implement it?
- What is the difference between `StringBuilder` and `StringBuffer`?
- How does autoboxing and unboxing work?
- What is the difference between `HashMap` and `Hashtable`?
- What is a marker interface?
- Explain the `Object` class methods.
- What is the difference between `equals()` and `hashCode()` contract?
- What is method reference in Java?
- What is the `instanceof` operator?
- Explain the difference between `throw` and `throws`.
- What is the difference between `final`, `finally`, and `finalize`?
- What is a static block and when is it used?
- Can a constructor be `private`? Why?
- What is the difference between `int` and `Integer`?
- What is a `record` in Java (Java 16+)?
- What is shadowing in Java?
- Explain covariance and contravariance in Java.
- What is the Difference Between Deep Copy and Shallow Copy in Java?
- What is the `transient` keyword?
- What is the difference between `Comparable` and `Comparator`?
- What is the purpose of the `volatile` keyword?
- What is the difference between `Stack` and `Heap` memory?
- What are enums and when to use them?
- What is reflection in Java?
- What is the difference between `public static void main(String[] args)` components?
- What is a nested class vs inner class?
- What is the difference between `null` and empty string?
- What is the diamond problem and how does Java avoid it?
- What is a lambda expression?
- What is a functional interface?
- What is the difference between `sleep()` and `wait()`?
- What is the difference between `ArrayList` and `LinkedList`?
- What is the `super` keyword used for in constructors?
- What is the difference between compile-time and runtime polymorphism?
- What is the purpose of `@Override` annotation?
- What is a deadlock and how to avoid it?
- What is the difference between `Error` and `Exception`?
- What is the `assert` keyword?
- What is the difference between `var` (Java 10+) and explicit type?
- What is a strong reference vs weak reference?
- What is the `strictfp` keyword?
- What is the difference between `import` and `static import`?
- What is a sealed class (Java 17)?
- What is the difference between `ExecutionException` and `InterruptedException`?
- What is the difference between a mutable and immutable object?
Related
- Spring Boot interview questions
- Low-level design (LLD) interview questions
- SDE-2 interview preparation guide
- Practice coding problems online
Frequently asked questions
What are the most asked Java interview topics?
Object-oriented design, the collections framework (HashMap internals especially), multithreading and the java.util.concurrent package, exceptions, the JVM memory model and garbage collection, Java 8 streams and lambdas, and Spring for backend roles.
How should I prepare for a Java interview in a week?
Spend the first days on collections, OOP and exceptions, then concurrency and JVM internals, and finish with a few coding problems and one design question. Reading each answer and then explaining it aloud works better than rereading.
Do I need to know Spring for a Java interview?
For most backend roles, yes: dependency injection, how a request flows through a Spring Boot application, transactions and configuration. See the Spring Boot questions linked on this page.
Are these Java questions free?
A sample is free to read without paying. Create a free account to unlock more, and Premium unlocks every answer.