Java interview questions, with clear answers
Java is still the most common language in fresher technical rounds, especially at IT services companies. Expect core Java — the JVM, Strings, collections, exceptions and a little multithreading — plus object-oriented programming, which has its own page.
The answers below are what a strong fresher would say: the direct answer first, then the one detail that shows real understanding.
Sample answers are examples written by Interview Fury. Adapt them to your own experience and say them in your own words.
16 questions on this page
1.What is the difference between JDK, JRE and JVM?
Why they ask: It's the standard warm-up question in a Java round.
The JVM (Java Virtual Machine) runs Java bytecode: it loads classes, verifies them, and executes them by interpreting and JIT-compiling them to native code. The JRE (Java Runtime Environment) is the JVM plus the standard class libraries — everything needed to run a Java program. The JDK (Java Development Kit) is the runtime plus the development tools, such as the javac compiler, jar and the debugger — what you need to write programs.
Since Java 11 there's no separate JRE download; the JDK includes the runtime.
2.Why is Java platform independent?
Because the compiler doesn't produce machine code for one kind of processor. It produces bytecode (.class files) for the JVM. Every operating system has its own JVM that runs the same bytecode, so a compiled program runs on Windows, Linux or macOS unchanged — "write once, run anywhere". Note that the JVM itself is platform dependent; it's the bytecode that isn't.
3.Why are Strings immutable in Java?
Why they ask: It checks whether you understand design decisions, not just syntax.
Once created, a String object can't change; methods like concat() or toUpperCase() return a new String. The reasons:
- The string pool — string literals are shared, which is only safe if nobody can modify them.
- Security — strings hold file paths, URLs and class names, which mustn't change after they've been checked.
- Thread safety — immutable objects can be shared between threads without locks.
- Hash caching — the hash code is computed once, which makes Strings fast
HashMapkeys.
4.What is the difference between String, StringBuilder and StringBuffer?
String is immutable, so building a string piece by piece in a loop creates many temporary objects. StringBuilder is mutable and fast — use it whenever you build a string step by step. StringBuffer is also mutable, but its methods are synchronised (thread-safe), which makes it slower; it's rarely needed today, because a string is seldom built by several threads at once.
5.What is the difference between == and equals()?
For objects, == compares references — whether two variables point to the same object. (For primitives, it compares values.) equals() compares content, as defined by the class.
So new String("hi") == new String("hi") is false, while .equals() returns true. If you override equals(), you must also override hashCode() so that equal objects have equal hash codes; otherwise HashMap and HashSet behave incorrectly.
Practise saying it, not just reading it
Rehearse these answers out loud with real-time AI help, tailored to your resume and the job description. Free minutes every day — no card required.
Try it free6.How does HashMap work internally?
Why they ask: It's the favourite "deep" Java question for freshers.
A HashMap keeps an array of buckets. To store an entry, it computes the key's hashCode(), mixes the bits, and uses the result to choose a bucket. Entries whose keys land in the same bucket are kept in a linked list. Since Java 8, a bucket that grows beyond 8 entries — once the table has at least 64 buckets — is converted into a red-black tree, so the worst case is O(log n) rather than O(n).
On get(), it finds the bucket the same way, then compares keys with equals(). When the number of entries exceeds capacity × load factor (16 × 0.75 by default), the array doubles in size and the entries are redistributed. A HashMap allows one null key and isn't thread-safe; use ConcurrentHashMap when several threads share it.
7.What is the difference between ArrayList and LinkedList?
ArrayList is backed by a resizable array: get(i) is O(1) and iteration is fast, but inserting or removing in the middle shifts the elements that follow, which is O(n). LinkedList is a doubly linked list: adding or removing at either end is O(1), but get(i) has to walk the list (O(n)), and every node uses extra memory.
In practice ArrayList is the default choice, and ArrayDeque usually beats LinkedList when you need a queue or a stack.
8.What is the difference between HashMap, LinkedHashMap and TreeMap?
All three implement Map. HashMap has no guaranteed order and O(1) average operations. LinkedHashMap remembers insertion order — or access order, which makes it handy for an LRU cache — at a small extra cost. TreeMap keeps its keys sorted, by natural order or a Comparator, with O(log n) operations and range methods such as headMap() and ceilingKey().
9.What are checked and unchecked exceptions?
Checked exceptions are checked at compile time: a method must either catch them or declare them with throws. Examples are IOException and SQLException, which represent recoverable problems outside the program's control.
Unchecked exceptions extend RuntimeException — for example NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException — and usually point to bugs; the compiler doesn't force you to handle them. Errors such as OutOfMemoryError are also unchecked, and normally shouldn't be caught.
10.What is the difference between final, finally and finalize?
finalis a keyword. A final variable can't be reassigned, a final method can't be overridden, and a final class can't be extended.finallyis a block aftertryandcatchthat always runs, typically to release resources — though try-with-resources is the modern way to do that.finalize()was a method the garbage collector might call before reclaiming an object. It was unreliable and has been deprecated since Java 9, so don't use it.
11.What does the static keyword mean?
static makes a member belong to the class rather than to any object. A static variable is shared by all instances. A static method can be called without an object, as in Math.abs(), and can't use this or instance fields directly. A static block runs once, when the class is loaded. And a static nested class doesn't need an instance of the outer class. main is static so the JVM can call it without creating an object first.
12.How does garbage collection work in Java?
The JVM automatically frees objects that can no longer be reached from any live reference — local variables, static fields or running threads.
The heap is generational. New objects go into the young generation; most of them die young, so it's collected often and cheaply (a minor GC). Objects that survive several collections move to the old generation, which is collected less often. Modern JVMs use collectors such as G1, the default since Java 9, or ZGC for very short pauses. System.gc() is only a request, and memory leaks can still happen if you hold references by mistake — for example, objects kept forever in a static collection.
13.What is the difference between Thread and Runnable?
You can create a thread by extending Thread and overriding run(), or by implementing Runnable and passing it to a Thread. Implementing Runnable is preferred: your class can still extend another class, the task stays separate from the thread that runs it, and the same task can be submitted to an ExecutorService. In real code you'd use executors (thread pools) rather than creating threads by hand.
14.What does synchronized do?
synchronized runs a method or block while holding a lock — the object's monitor — so only one thread at a time can execute it on that object. It prevents race conditions on shared data, and it also guarantees that changes made inside are visible to the next thread that takes the lock.
Overusing it hurts performance and can cause deadlocks, so keep synchronised sections small. Alternatives include ReentrantLock, atomic classes such as AtomicInteger, and the concurrent collections.
15.What are the main features of Java 8?
- Lambda expressions — short inline functions, such as
list.forEach(x -> System.out.println(x)) - Functional interfaces — interfaces with one abstract method, like
Predicate,FunctionandSupplier - The Stream API — filter, map and reduce collections declaratively:
List<String> adults = users.stream()
.filter(u -> u.getAge() >= 18)
.map(User::getName)
.collect(Collectors.toList());
- Optional — an explicit "may or may not have a value" instead of returning null
- Default and static methods in interfaces, and the java.time date and time API
Mentioning newer features you've actually used — records, var, switch expressions or virtual threads (Java 21) — is a plus.
16.Can you override a static method or a private method?
No. Static methods belong to the class, so a subclass method with the same signature hides the parent's method rather than overriding it; which one runs depends on the reference type, not the object. Private methods aren't visible to subclasses at all, so a method with the same name in the child is a new, unrelated method.
Prepare with Interview Fury
Rehearse these answers out loud with real-time AI help, tailored to your resume and the job description. Free minutes every day — no card required.
Try it free