interview questions
Java interview questions and answers for freshers
The Java questions that come up repeatedly in Indian hiring, grouped by what is actually being probed, with the follow-up that usually separates candidates.
By the Samyak faculty team · Published · 10 min read
Java interviews in India are unusually predictable. The same areas recur across services companies, product firms and startups, and they are chosen because they reveal whether someone understands the language or has memorised it.
Grouped by what is being tested, the preparation becomes much more efficient.
Collections — the most-weighted area
ArrayList versus LinkedList — which and why?
ArrayList is backed by an array, giving constant-time access by index and expensive insertion at the front. LinkedList gives constant-time insertion at the ends and linear-time access by index.
The answer interviewers want: ArrayList almost always, because real code reads far more than it inserts, and array locality makes it faster in practice even for operations where LinkedList looks better on paper. Choosing LinkedList reflexively because “insertion is O(1)” is the trap.
How does HashMap work internally?
An array of buckets. The key’s hashCode determines the bucket, equals resolves collisions within it. Modern Java converts a bucket to a balanced tree once collisions exceed a threshold, which bounds worst-case lookup.
Expect the follow-up: what happens if a key class has a bad hashCode? Every entry lands in one bucket and lookup degrades toward linear. This is why the hashCode contract matters.
Explain the equals and hashCode contract.
If two objects are equal, they must have the same hashCode. The reverse is not required. Break this and hash-based collections misbehave — you insert an object and cannot find it again.
Be ready to demonstrate it. A mutable field used in hashCode, changed after insertion into a HashSet, makes the object effectively unreachable. That demonstration lands better than the definition.
HashMap versus HashSet versus HashTable?
HashSet is a HashMap with a dummy value. HashTable is the legacy synchronised version — do not use it; use ConcurrentHashMap when you need thread safety.
When would you use TreeMap over HashMap?
When you need keys in sorted order or range queries. You pay logarithmic rather than constant lookup for that ordering, so only take it when the ordering is actually used.
Object-oriented design
Abstract class or interface — how do you choose?
Interface when you are defining a capability that unrelated types can have. Abstract class when you are sharing state and partial implementation among closely related types.
Modern follow-up: since interfaces now allow default methods, what is left of the distinction? State. An abstract class can hold fields; an interface cannot.
Why is composition usually preferred over inheritance?
Inheritance couples the subclass to the parent’s implementation, so a change upstream can break it in ways the compiler will not catch. It also forces a single hierarchy. Composition lets you assemble behaviour and swap it.
Have a concrete example. Inheriting from ArrayList to make a Stack exposes every list operation, which is exactly what a stack should not offer.
What is polymorphism, in practice rather than definition?
Writing code against a type and having the correct implementation chosen at runtime. The practical value is that adding a new implementation requires no change to the calling code.
Interviewers ask this constantly and most candidates recite a definition. Answering with what it buys you is noticeably better.
Can you override a static method?
No. Static methods belong to the class and are hidden rather than overridden, so resolution happens at compile time based on the reference type. This is a favourite trick question.
Strings
Why are strings immutable in Java?
Security, thread safety, hashcode caching, and the string pool. If strings were mutable, a string used as a HashMap key or a file path could be changed after validation.
What is the string pool?
A cache of string literals in the heap. Two identical literals reference the same
object; new String("a") deliberately creates a distinct one.
Expect: what does s1 == s2 return for two identical literals? True, because of
the pool. And for new String versions? False. And what should you use instead?
equals, always, for content comparison.
String, StringBuilder or StringBuffer?
String for values that do not change. StringBuilder for building strings, particularly in loops. StringBuffer is the synchronised version and is rarely needed.
Follow-up: why is concatenating strings in a loop bad? Each concatenation creates a new object, making it quadratic. This appears in real code constantly.
Exceptions
Checked versus unchecked?
Checked exceptions must be declared or handled and represent recoverable conditions. Unchecked extend RuntimeException and usually represent programming errors.
Be ready for the opinion question: are checked exceptions a good idea? There is genuine disagreement, and having a reasoned position — usually that they are overused and lead to empty catch blocks — reads as maturity.
What does finally do, and when does it not run?
It runs whether or not an exception was thrown, and is used for cleanup. It does not run if the JVM exits or the thread is killed.
Follow-up: what is better than finally for closing resources? try-with-resources, which is less error-prone and closes in reverse order automatically.
Why is catching Exception broadly a problem?
It swallows errors you needed to see, including programming bugs, and makes failures silent. Catch the specific exception you can actually handle.
Java 8 and later
What is a lambda, and what is a functional interface?
A lambda is a concise implementation of a single-method interface. A functional interface is one with exactly one abstract method, which is what makes lambdas possible.
What are streams, and when are they wrong?
A declarative pipeline over a collection. They are worth using for readable transformation chains.
Where they are wrong: simple loops where a stream adds indirection, cases needing early exit with complex conditions, and hot paths where the overhead matters. Being able to say when not to use them is the differentiator.
What problem does Optional solve?
It makes absence explicit in a method signature rather than returning null and hoping the caller checks. Note the caveat — using Optional as a field or a method parameter is generally considered misuse; it is intended for return types.
The question that decides it
Walk me through a project you built.
Two minutes, prepared. What it does, one design decision and why, one thing you would do differently.
For Java roles specifically, be ready to be asked why you structured your classes the way you did. That question is where the object-oriented design module either pays off or exposes that it did not land.