Java interview questions and answers
Java is the language most Indian services companies hire on, and it is still the backbone of banking, insurance and enterprise product teams. Fresher rounds stay on core language and collections. Once you have two years behind you, the interview moves to concurrency, the JVM, and whatever Spring Boot code you have actually shipped.
Asked of: Java developers, backend engineers, and almost every fresher in a services company. Every answer below describes what a strong response covers rather than a script to memorise, because the follow-up question is where these rounds are actually decided.
Core language and OOP
JDK, JRE and JVM, the four OOP pillars, and how you used them in code you wrote yourself. Definitions alone are half marks.
Collections
ArrayList against LinkedList, how HashMap works inside, and the equals and hashCode contract. This is the most reliably asked area at every level.
Exceptions and generics
Checked against unchecked, what belongs in a catch block, and why generics exist at all once you know about type erasure.
Concurrency and the JVM
Threads, volatile, synchronized and the collections built for concurrent access. Expect this from about two years of experience onwards.
Spring Boot and JPA
Most experienced Java interviews in India spend more minutes here than on core Java. Dependency injection, transactions, and the queries your ORM generates behind your back.
Java interview questions for freshers
What is the difference between the JDK, the JRE and the JVM?
The JVM runs bytecode, the JRE is the JVM plus the standard libraries, and the JDK is the JRE plus the compiler and tools you need to build. Finish the answer by connecting it to portability: your class file runs anywhere because each platform ships its own JVM.
What is the difference between == and .equals()?
For objects, == compares references and equals compares content once a class overrides it. The follow-up is usually about strings, where two literals share one pooled object and two new String objects do not, so == can look correct by accident.
Why is String immutable in Java?
String pooling, a cached hash code, thread safety, and safe use as a key or in security-sensitive values. Then say what you use instead when you are building text in a loop, which is StringBuilder.
ArrayList or LinkedList, and why?
A backing array with O(1) indexed access against nodes with O(1) insertion once you already hold the node. Say the honest practical part too: ArrayList wins most real cases because contiguous memory reads faster, and LinkedList rarely earns its place.
How does a HashMap work internally?
The key's hash decides a bucket, collisions go into a chain, and from Java 8 a long chain turns into a balanced tree. Mention the 0.75 load factor and resizing. The interviewer is really asking whether you know why hashCode and equals both matter.
What is the difference between overloading and overriding?
Overloading is resolved at compile time from the signature, overriding at runtime from the object's actual type. The trap in the follow-up is a static method, which is hidden rather than overridden.
Interface or abstract class?
An abstract class carries shared state and partial implementation, an interface carries a contract that a class can take on alongside others. Since Java 8 an interface can also hold default methods, so say which Java version your answer assumes.
Explain final, finally and finalize.
A keyword that prevents reassignment, extension or overriding, a block that runs whether or not an exception was thrown, and a deprecated hook you should never rely on. Short and precise beats long here.
Checked or unchecked exceptions, and when do you use each?
Checked exceptions are enforced by the compiler and suit recoverable failures at a boundary, such as a file or a network call. Unchecked ones signal programming errors. Say plainly that catching an exception and doing nothing with it is worse than letting it travel.
Can you override a static method?
No. You can declare the same signature in a subclass, but that hides the method rather than overriding it, and the call resolves from the reference type instead of the object. This question is checking whether you understand runtime dispatch.
Java interview questions for experienced candidates
What happens when two threads write to the same HashMap?
You can lose entries, and on older versions you could spin a thread on a corrupted chain during resize. Move to ConcurrentHashMap, and explain why it beats a synchronized wrapper: it locks a small part of the table rather than the whole map.
What is the equals and hashCode contract, and what breaks when you ignore it?
Equal objects must return the same hash code. Break it and an object you just put into a HashSet is not found again. The second half of the answer is mutable keys: change a field used by hashCode after insertion and the entry is effectively lost.
Walk me through how garbage collection works.
Young and old generations, minor collections copying survivors, and a major collection being the expensive one. Name the collector you have actually run with. Then say where leaks really come from in Java, which is usually a static collection nobody clears.
Streams: what is lazy, and when would you avoid them?
Intermediate operations build a pipeline and nothing runs until a terminal operation asks for a result. A stream cannot be reused. On parallel streams, say that they borrow the common ForkJoinPool, which is a real problem in a request-handling thread.
What is Optional actually for?
Expressing that a return value may be absent, so the caller has to deal with it. Say where it does not belong: fields, parameters, and any code path where you end up calling get() without checking, which just moves the null problem.
volatile or synchronized?
volatile guarantees visibility of a write to other threads. synchronized also gives mutual exclusion. A status flag is fine with volatile. A counter is not, because incrementing reads and writes, and two threads can interleave inside that.
Comparable or Comparator?
Comparable puts one natural ordering inside the class. Comparator keeps ordering outside, so you can have several and change them per call site. If you have used Comparator.comparing with thenComparing, show that, because it tells the interviewer you have sorted something real.
How does dependency injection work in Spring, and which scope do you use?
The container builds and wires beans, and constructor injection makes the dependency obvious and the object testable. Singleton is the default, so shared mutable state on a bean is a bug waiting for load. Mention how you have handled a circular dependency.
What are the traps with @Transactional?
It works through a proxy, so calling an annotated method from inside the same class skips it entirely. Rollback happens on unchecked exceptions by default, and a checked exception needs rollbackFor. This question separates people who have used Spring from people who have debugged it.
You see hundreds of queries for one page load. What is happening?
The N+1 problem: one query for the parent rows, then one per row for a lazy association. Fix it with a join fetch or an entity graph. Add what LazyInitializationException means, because it is the same subject from the other side.
- Services companies almost always start from your project, not from a definition. Know the part you did not personally build well enough to describe it anyway.
- Say which Java version you use daily. Claiming records and sealed classes and then writing Java 8 code loses more trust than admitting the version.
- Write the code even when you are unsure of the syntax. A blank sheet scores nothing, and interviewers correct syntax happily.
- If your day job is Spring Boot rather than core Java, say so early and steer the conversation to what you have shipped.
Answer them out loud before someone asks
Reading a question and answering it under a follow-up are different skills. Practise these against an AI interviewer that pushes back and scores your answer, or check your resume first with the free ATS resume checker.
Start My Free Mock Interview