JAVA INTERVIEW QUESTIONS
Java
Interview Questions
Java interview questions: review frequently asked technical interview questions, with explained answers, common pitfalls, OOP, collections, exceptions, JVM, HashMap, equals(), hashCode(), and streams.
WHY REVIEW JAVA?
In an interview, knowing Java is not enough.
You need to know how to explain it.
In a Java interview, the interviewer isn't just checking if you know the syntax. They mainly want to know if you understand the language's core concepts and can explain them clearly out loud.
This Java interview questions page helps you review the topics that frequently come up with an interviewer, a senior developer, or a tech lead: object-oriented programming, collections, exceptions, JVM, memory, String, HashMap, equals(), hashCode(), streams, and best practices.
To go further, DevUpNow lets you practice with Java interview questions, explained answers, cheat sheets, and common pitfalls to better prepare for your technical interview.
TOPICS TO REVIEW
Java topics that frequently come up in interviews
OOP
Class, object, inheritance, interface, abstraction, and polymorphism.
Collections
List, Set, Map, HashMap, ArrayList, HashSet, and TreeMap.
equals / hashCode
Understand logical equality and hash-based collections.
JVM & memory
Stack, heap, garbage collector, JDK, JRE, and JVM.
FREQUENTLY ASKED QUESTIONS
Java interview questions with explained answers
These Java interview questions are designed to help you answer clearly, avoid common pitfalls, and better structure your explanations during a technical interview.
1. What is the difference between == and equals() in Java?
This is a classic Java interview question because it verifies whether the candidate understands the difference between reference comparison and logical comparison.
The operator == compares references when used with objects. It checks
whether two variables point to the same object in memory.
The method equals(), on the other hand, is used to compare logical equality between two objects.
For example, two different String objects can contain the same text.
In this case, equals() can return true, whereas ==
can return false.
== compares references, while equals()
compares logical content if the method is properly overridden.
2. Why override equals() and hashCode() together?
This is a very common Java interview question because it verifies whether the candidate understands
how hash-based collections work, such as HashMap or HashSet.
The core rule is simple: if two objects are equal according to equals(), they must have
the same hashCode().
In an interview, you should explain that equals() defines logical or business equality, while
hashCode() helps certain collections retrieve objects quickly.
If hashCode() is poorly implemented, a HashSet or a HashMap
might fail to correctly find an object even though it is equal according to equals().
equals(), you must almost always override
hashCode() as well.
3. What is the difference between an interface and an abstract class?
An interface primarily defines a contract. It specifies what a class must be able to do, without necessarily enforcing a complete implementation.
An abstract class allows you to factor out common behavior across multiple related classes. It can contain abstract methods, concrete methods, and shared state.
Since Java 8, interfaces can also contain methods default and static.
Despite this, interfaces are still mainly used to express a capability or a contract.
4. What is the difference between overloading and overriding?
Overloading consists of defining multiple methods with the same name but different parameters within the same class.
Overriding consists of providing a new implementation for a method inherited from a parent class or an interface.
In an interview, make sure to emphasize that overloading is resolved at compile time based on the method signature, whereas overriding is resolved at runtime through polymorphism.
5. What is the difference between ArrayList and LinkedList?
ArrayList uses a dynamic array internally. It is generally very efficient for accessing
an element by index because access is direct.
LinkedList uses a doubly linked list. Accessing an element by index is often less performant
because it requires traversing the list.
In practice, ArrayList is the default choice in most cases. LinkedList
can be useful in specific scenarios, but it is used much less frequently in standard applications.
ArrayList is the best default choice.
6. How does a HashMap work in Java?
HashMap stores data as key-value pairs. To quickly retrieve a value,
it uses the key's hashCode() to determine an internal bucket location.
When multiple keys resolve to the same location, it is called a collision. Java handles these collisions using a suitable internal data structure.
In an interview, you should explain that the proper functioning of a HashMap heavily depends on the
consistency between equals() and hashCode().
HashMap uses hashCode() to quickly locate a key,
and then equals() to verify equality.
7. What is the difference between a checked exception and an unchecked exception?
A checked exception is verified at compile time. The developer must either catch it with
try/catch, or declare it with throws.
An unchecked exception inherits from RuntimeException. It is not enforced by the compiler and
usually occurs at runtime.
For example, IOException is a checked exception, whereas NullPointerException
or IllegalArgumentException are unchecked exceptions.
8. What is the difference between stack and heap in Java?
The stack notably contains method calls, local variables, and primitive local references.
The heap contains dynamically created objects, particularly those instantiated with new. This is also the memory area
where the garbage collector operates to reclaim unused objects.
In an interview, avoid saying that everything is either strictly in the stack or strictly in the heap. A local variable reference may live on the stack, but the referenced object typically resides in the heap.
Want to practice more Java interview questions?
DevUpNow helps you prepare for Java, Spring Boot, Hibernate, and SQL interview questions with detailed explanations and common pitfalls.
Download the app for free βTRICK QUESTIONS
Common Java trick questions in technical interviews
Some Java questions look simple, but they test whether you truly understand the language. Here are a few classic pitfalls to review before an interview:
String and ==
Why can == sometimes return true with certain Strings?
HashMap
What happens if equals() is overridden without hashCode()?
Streams
Is parallelStream() always faster than stream()?
CONTINUE PREPARATION
Continue your Java/Spring preparation
After this Java interview questions page, strengthen other key topics frequently asked in backend interviews.
FAQ
FAQ β Java Interview Questions
What are the most common Java interview questions?
The most common Java interview questions cover object-oriented programming, collections,
equals() and hashCode(), exceptions, the JVM, Strings, streams,
lambdas, and best development practices.
How to prepare for a junior Java interview?
To prepare for a junior Java interview, you need to master language fundamentals, understand core concepts, and practice explaining your answers clearly. Priority topics include OOP, collections, exceptions, String, HashMap, interfaces, and inheritance.
Do you need to know Spring Boot for a Java interview?
For a Java backend role, Spring Boot is often required. However, core Java fundamentals remain essential, as many Spring questions rely on Java concepts like interfaces, exceptions, annotations, collections, and object management.
How many questions should you review before a Java interview?
It is better to master 30 to 50 key questions with clear explanations than to read 200 questions without being able to explain them. In an interview, the quality of your explanation matters as much as raw knowledge.
Ready to ace
your next Java interview?
Google Play
the App Store
Download DevUpNow
Choose your store to start for free.