Top 10 Java Interview Questions with Clear Answers
Updated 28 Oct 2026 4 min read
On this page 12 sections
- 1What is the difference between JDK, JRE and JVM?
- 2What is the difference between == and .equals()?
- 3Why is String immutable?
- 4Abstract class vs interface
- 5Method overloading vs overriding
- 6How does HashMap work internally?
- 7ArrayList vs LinkedList
- 8Checked vs unchecked exceptions
- 9What is garbage collection?
- 10How do you create a thread, and what does synchronized do?
- 11How to use these answers
- 12What next
These are the questions interviewers ask most for Java roles. Do not memorise the answers word for word. Understand each idea, run the code and then explain it in your own words. For a broader overview see the Java guide for placements.
1. What is the difference between JDK, JRE and JVM?
- JVM (Java Virtual Machine) runs compiled bytecode. It makes Java platform independent.
- JRE (Java Runtime Environment) is the JVM plus the standard libraries needed to run Java programs.
- JDK (Java Development Kit) is the JRE plus tools to write and compile code, such as the compiler (
javac) and debugger.
2. What is the difference between == and .equals()?
== compares references for objects (whether both variables point to the same object), and values for primitives. .equals() compares content when the class overrides it, as String does.
String a = new String("java");
String b = new String("java");
System.out.println(a == b); // false: different objects
System.out.println(a.equals(b)); // true: same characters
3. Why is String immutable?
Once created, a String cannot change. Benefits: it is safe to share between threads, it can be cached in the string pool, its hash code can be cached (useful as a map key) and it is more secure because values such as file paths cannot be altered after validation. For many changes in a loop use StringBuilder.
4. Abstract class vs interface
| Abstract class | Interface | |
|---|---|---|
| Methods | Can have implemented and abstract methods | Abstract by default, can have default and static methods |
| State | Can have instance fields | Only constants |
| Inheritance | A class extends one abstract class | A class can implement many interfaces |
| Use when | Related classes share code and state | Unrelated classes share a capability |
5. Method overloading vs overriding
- Overloading: same method name, different parameters, in the same class. Resolved at compile time.
- Overriding: a subclass provides its own version of a parent method with the same signature. Resolved at run time (polymorphism).
class Animal { void sound() { System.out.println("..."); } }
class Dog extends Animal { @Override void sound() { System.out.println("Woof"); } }
Animal a = new Dog();
a.sound(); // Woof: decided by the real object, at run time
6. How does HashMap work internally?
It stores entries in an array of buckets. The key's hashCode() decides the bucket. If several keys land in the same bucket (a collision), they are chained (as a linked list, and as a balanced tree when the chain gets long). On lookup, Java finds the bucket by hash and then uses equals() to find the exact key. When the map becomes too full (load factor, default 0.75), it resizes and rehashes. That is why keys need a consistent equals() and hashCode(). Average get and put are O(1).
7. ArrayList vs LinkedList
ArrayList is backed by an array: fast random access (O(1)), slow insertion or removal in the middle (O(n)). LinkedList is a doubly linked list: fast insertion at the ends or when you hold a node, but slow access by index (O(n)). In practice ArrayList is the right default.
8. Checked vs unchecked exceptions
- Checked exceptions (for example
IOException,SQLException) are verified at compile time; you must catch them or declare them withthrows. - Unchecked exceptions extend
RuntimeException(for exampleNullPointerException,ArrayIndexOutOfBoundsException) and usually indicate programming errors.
try (java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader("data.txt"))) {
System.out.println(br.readLine());
} catch (java.io.IOException e) {
System.out.println("Could not read file");
}
The try-with-resources form closes the reader automatically.
9. What is garbage collection?
Java automatically frees memory for objects that are no longer reachable from running code. The garbage collector runs in the background, so you do not call free() as in C. You can suggest a collection with System.gc(), but the JVM decides. Memory leaks can still happen if you keep references to objects you no longer need, such as in a static collection.
10. How do you create a thread, and what does synchronized do?
Create a thread by implementing Runnable (preferred) or extending Thread:
Runnable task = () -> System.out.println("Running in " + Thread.currentThread().getName());
Thread t = new Thread(task);
t.start(); // start(), not run()
When several threads change shared data, you can get race conditions. synchronized allows only one thread at a time into a block or method for the same lock, which keeps the data consistent.
class Counter {
private int count = 0;
public synchronized void increment() { count++; }
public synchronized int get() { return count; }
}
How to use these answers
- Run each code sample and change it
- Explain it aloud in under 60 seconds
- Prepare one follow-up for each (for example, "what is the load factor?")
- Practise related problems from the DSA study plan
What next
Learn more interview basics in core CS subjects and OOP and design principles, and look for Java roles among our fresher and experienced jobs.
Found this useful? Share it with a friend.