Java Interview Question Bank Enhanced
Java Interview Question Bank Enhanced
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 1
HOW TO USE THIS QUESTION BANK
■ Must-Know Asked in 80%+ real interviews. Master these before any technical round.
■ Medium Frequently asked for 1-3 year experience roles. Strong differentiators.
[NEW] Newly added question not present in the original question bank.
This bank spans 12 topic sections with 380+ non-duplicate, interview-tested questions. All questions have been
filtered for real-world relevance. New questions added in this edition fill identified gaps in Spring AOP/Proxy
internals, @Transactional deep behavior, Spring Boot Testing (a complete new section), SQL execution plans, and
advanced debugging scenarios.
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 2
SECTION 1 — CORE JAVA
OOP Fundamentals
1. ■ What are the four pillars of OOP? Give one real-world backend example for each.
2. ■ What is the difference between method overloading and method overriding? Can you override a static
method?
3. ■ What is the difference between an abstract class and an interface in Java 8+? When do you choose one
over the other?
4. ■ What is runtime polymorphism? Give a Spring backend example where it is actually used.
5. ■ What is encapsulation and why is it critical in a REST service?
6. ■ What is the difference between 'is-a' and 'has-a' relationships? Which leads to tighter coupling?
7. ■ What is constructor chaining? Show how this() and super() behave with a concrete example.
8. ■ What is the 'final' keyword? What are all the contexts it can be applied in and what does each mean?
9. ■ What is the difference between == and .equals() for Strings? Why does == sometimes return true for
String literals?
10. ■ What is pass-by-value in Java? Prove using an object mutation example that Java never passes by
reference.
11. ■ Explain the SOLID principles. Give a Spring Boot example for each.
12. ■ What is the difference between composition and inheritance? Why is composition generally preferred?
13. ■ What is the Liskov Substitution Principle? Give a clear violation example.
14. ■ What is an immutable class? List every condition required to make one correctly, including mutable
field protection.
15. ■ What is double-checked locking in Singleton? Write a correct thread-safe Singleton using volatile.
16. ■ What is the difference between shallow copy and deep copy? How do you implement deep cloning?
17. ■ What is autoboxing/unboxing? Where can it cause a NullPointerException or performance issue?
18. ■ What is covariant return type in method overriding? Give a practical example.
19. ❄ How do you protect an immutable class from being broken via reflection?
Strings
1. ■ Why is String immutable in Java? What are the security and performance benefits?
2. ■ What is the String pool? When is a String added to the pool vs allocated on the heap?
3. ■ What is the difference between String, StringBuilder, and StringBuffer? When do you use each?
4. ■ How many objects are created: String s = new String("hello");? Explain each object created.
5. ■ What is the output: String s = "Hello"; [Link](" World"); [Link](s); — and why?
6. ■ Why should you use StringBuilder instead of + concatenation inside a loop?
7. ■ How many objects: String s1 = "hello"; String s2 = "hello"; — and which == comparisons are true?
8. ■ What is [Link]() and when would you use it in a real application?
Exception Handling
1. ■ What is the difference between checked and unchecked exceptions? Give two examples of each.
2. ■ What is the difference between Error and Exception? Should you ever catch an Error?
3. ■ What does a finally block guarantee? Can it ever be skipped?
4. ■ Can a finally block override a return statement in the try block? What is the actual output?
5. ■ What is try-with-resources? Why is it preferred over manual finally-based cleanup?
6. ■ How do you create a meaningful custom exception? Show checked vs unchecked variants.
7. ■ When should you use throws vs wrapping in try-catch? What is exception propagation?
8. ■ What is exception chaining (getCause)? Why is it important for production debugging?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 3
9. ■ [NEW] What are the dangers of catching Exception or Throwable broadly in a Spring service?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 4
SECTION 2 — JVM & MEMORY INTERNALS
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 5
SECTION 3 — COLLECTIONS
Core Collections
1. ■ What is the difference between ArrayList and LinkedList? When does LinkedList actually outperform
ArrayList?
2. ■ What is the difference between HashMap and TreeMap in terms of time complexity and iteration order?
3. ■ What is the difference between HashMap and LinkedHashMap? When would you use LinkedHashMap?
4. ■ What is the initial capacity and load factor of HashMap? How do they affect performance and memory?
5. ■ What happens when you add a duplicate key to a HashMap?
6. ■ What is the difference between fail-fast and fail-safe iterators? Give one example of each.
7. ■ What is the difference between Comparable and Comparator? How do you sort Employee objects by
salary?
8. ■ What is the difference between Stack and Deque? Why is Stack discouraged in Java?
9. ■ What is a PriorityQueue? How does it determine ordering and what is its time complexity for operations?
10. ■ What is a WeakHashMap? When and why would you use it?
11. ■ What is the difference between WeakReference, SoftReference, and StrongReference?
HashMap Internals
1. ■ Explain the internal working of HashMap: hashing, index generation, bucket array, collision handling,
treeification.
2. ■ What is the hashCode() and equals() contract? What breaks if you override only one?
3. ■ What happens when two keys have the same hashCode? How is collision resolved in Java 8+?
4. ■ At what threshold does a HashMap bucket convert from a linked list to a red-black tree? Why?
5. ■ Why are String and Integer preferred as HashMap keys? What makes a good map key?
6. ■ HashMap has load factor 0.75 and initial capacity 16. After how many entries does it resize?
7. ■ You put a mutable object as a HashMap key and then mutate it. What happens on the next get() call?
8. ■ How does ArrayList resize internally? What is the growth factor and amortized cost per insertion?
9. ■ What is the difference between HashMap and Hashtable? Why was Hashtable deprecated?
Concurrent Collections
1. ■ What is the difference between HashMap and ConcurrentHashMap?
2. ■ How does ConcurrentHashMap achieve thread safety without locking the entire map in Java 8?
3. ■ What changed in ConcurrentHashMap between Java 7 (segment locking) and Java 8 (CAS +
synchronized buckets)?
4. ■ What is CopyOnWriteArrayList? What is its trade-off and when should you use it?
5. ■ What is the difference between synchronized collections ([Link]) and concurrent
collections?
6. ■ When should you use [Link]() vs CopyOnWriteArrayList? Justify with a scenario.
7. ■ What is a NavigableMap? Name two implementations and a real use case for each.
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 6
SECTION 4 — MULTITHREADING & CONCURRENCY
Thread Basics
1. ■ What is the difference between a process and a thread? What is a context switch?
2. ■ What are the different ways to create a thread in Java? Which approach is preferred and why?
3. ■ What is the difference between Runnable and Callable? How do you retrieve a result from Callable?
4. ■ What is the thread lifecycle in Java? Describe all states and transitions.
5. ■ What is the difference between sleep() and wait() in terms of lock release behavior?
6. ■ What is a race condition? Give a simple bank account example.
7. ■ What is a deadlock? Give an example and explain three prevention strategies.
8. ■ What is volatile and when is it not sufficient to guarantee thread safety?
9. ■ Why are wait(), notify(), and notifyAll() defined in Object class and not in Thread class?
10. ■ What is the difference between notify() and notifyAll()? When can notify() cause a missed signal bug?
11. ■ What is a daemon thread? How do you create one and when is it useful?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 7
SECTION 5 — JAVA 8+
Streams
1. ■ How does a Stream pipeline work internally? What is lazy evaluation and why does it matter for
performance?
2. ■ What is the difference between intermediate and terminal operations? Give three examples of each.
3. ■ What is the difference between map() and flatMap()? Give a real example with a list of lists.
4. ■ How do you group a list of employees by department using Streams? Write the full pipeline.
5. ■ How do you find the employee with the highest salary using Streams?
6. ■ How do you remove duplicates from a list using Streams when the list has custom objects?
7. ■ What is the difference between findFirst() and findAny() in a parallel stream?
8. ■ When should you use parallelStream()? What are its pitfalls in a Spring web application?
9. ■ [NEW] Write a stream pipeline to find the top 3 highest-paid employees per department.
10. ■ What is the difference between peek() and map()? When is peek() useful for debugging?
11. ■ What is a Spliterator? How is it used internally by parallel streams?
12. ■ What is the difference between [Link]() and [Link]()?
13. ■ What is the difference between [Link]() and [Link]()?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 8
SECTION 6 — SPRING BOOT
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 9
2. ■ What is the difference between @RequestParam and @PathVariable? When do you use each?
3. ■ What HTTP status codes should you return for GET, POST, PUT, DELETE, and validation errors?
4. ■ What is the difference between PUT and PATCH? Which is idempotent?
5. ■ How does DispatcherServlet work in the Spring MVC request lifecycle? Walk through each step.
6. ■ How do you implement global exception handling using @ControllerAdvice?
7. ■ How do you validate a request body using @Valid and @Validated? What is the difference between
them?
8. ■ How do you handle MethodArgumentNotValidException and return a structured 400 error response?
9. ■ How do you configure CORS in Spring Boot to allow only your production frontend domain?
10. ■ How do you implement idempotency for a payment API endpoint?
11. ■ What is the difference between @ResponseBody and ResponseEntity?
12. ■ How do you implement API versioning in Spring Boot (URI, header, parameter approaches)?
13. ■ [NEW] How do you stream a 500MB file download without loading it fully into JVM memory?
14. ■ [NEW] How do you implement rate limiting in a Spring Boot REST API?
15. ❄ What is the Richardson Maturity Model and what are the four levels?
16. ❄ How does Spring Boot auto-configure Jackson? How do you customize date serialization?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 10
SECTION 7 — SPRING BOOT TESTING
Note: This entire section is new. Testing is consistently asked in 1-3 year experience interviews but was missing from the
original bank.
Mockito
1. ■ [NEW] What is Mockito? What is the difference between @Mock, @Spy, and @InjectMocks?
2. ■ [NEW] What is the difference between when().thenReturn() and doReturn().when()? When do you use
each?
3. ■ [NEW] How do you verify that a method was called with specific arguments using Mockito?
4. ■ [NEW] What is an ArgumentCaptor and when would you use it?
5. ■ [NEW] What is @MockBean vs @Mock? When do you use @MockBean in Spring Boot tests?
6. ■ [NEW] How do you mock a static method in Mockito 3.4+ using MockedStatic?
7. ■ [NEW] What is the difference between [Link]() and AssertJ assertions?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 11
SECTION 8 — JPA / HIBERNATE
Core Concepts
1. ■ What is the difference between JPA, Hibernate, and Spring Data JPA? What does each layer provide?
2. ■ What is the JPA persistence context? What is its default scope in a Spring Boot application?
3. ■ What are the four entity states in JPA (Transient, Managed, Detached, Removed)? How do you transition
between them?
4. ■ What is dirty checking in Hibernate? How does it work and when does the flush occur?
5. ■ What is [Link] vs [Link]? What is the default for @OneToMany and
@ManyToOne?
6. ■ What is the N+1 query problem? How do you detect it using Hibernate SQL logs and what are all the
ways to fix it?
7. ■ What is the difference between @JoinColumn and mappedBy in a bidirectional relationship?
8. ■ How do you avoid infinite recursion in JSON when using bidirectional JPA relationships?
9. ■ What is optimistic locking in JPA? How do you implement it with @Version?
10. ■ What are the GenerationType strategies? When would you use SEQUENCE over IDENTITY?
11. ■ What is the difference between JpaRepository and CrudRepository?
12. ■ How do you write a custom query using @Query? What additional annotation is needed for modifying
queries?
13. ■ [NEW] What is a LazyInitializationException? What are the three ways to fix it and which is preferred?
14. ■ [NEW] What is an Entity Graph? How do you use @EntityGraph to solve N+1 without over-fetching?
15. ■ What is pessimistic locking? When would you use PESSIMISTIC_WRITE vs PESSIMISTIC_READ?
16. ■ How do you implement pagination and sorting in Spring Data JPA?
17. ■ How do you implement batch inserts efficiently in JPA? What Hibernate properties must you enable?
18. ■ [NEW] How do you implement projections in Spring Data JPA? What is the difference between interface-based
and class-based projections?
19. ❄ How do you define a composite key using @EmbeddedId?
20. ❄ How do you implement auditing (@CreatedDate, @LastModifiedDate) in Spring Data JPA?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 12
SECTION 9 — SQL
Complex Queries
1. ■ Write a query to find the second highest salary from an Employee table without using LIMIT.
2. ■ Write a query to find the top 3 highest-paid employees per department using window functions.
3. ■ Write a query to find employees earning more than the average salary of their own department.
4. ■ What is a CTE (Common Table Expression)? How is it different from a subquery? Write a real example.
5. ■ What is a window function? Write a query using RANK(), DENSE_RANK(), and ROW_NUMBER() and
explain the difference between them.
6. ■ Write a query to detect duplicate rows in a table and delete all but one copy.
7. ■ Write a query to find employees who have the same salary as someone in a different department.
8. ■ Write a recursive CTE to traverse an employee-manager hierarchy up to the root CEO.
9. ■ Write a query using LEAD() and LAG() to calculate month-over-month revenue growth.
10. ❄ What is a PIVOT query? Write one that converts monthly sales rows into a single row with one column per
month.
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 13
14. ❄ Can a table exist without a primary key? What are the production implications (e.g., for replication)?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 14
SECTION 10 — MICROSERVICES
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 15
SECTION 11 — DEBUGGING & PRODUCTION SCENARIOS
Note: Scenario questions are the highest-signal questions in 1-3 year interviews. Practice structured root-cause analysis:
Observe, Hypothesize, Investigate, Fix.
Concurrency Issues
1. ■ Two threads update the same account balance simultaneously and some updates are lost. What is this
problem and how do you fix it in Spring Boot (DB locking, synchronized, AtomicLong)?
2. ■ A singleton Spring bean has an instance variable modified per-request. Multiple requests hit it and the
value is wrong. How do you fix it?
3. ■ A microservice uses ThreadLocal for user session in a thread pool. Wrong users see wrong session
data. What is the bug and the fix?
4. ■ Your ExecutorService thread pool is exhausted and requests are queuing forever. How do you diagnose
it and what parameters do you tune?
5. ■ A deadlock occurs between two service methods that lock two DB rows in different orders. How do you
detect it and prevent it?
6. ■ Your service uses parallelStream() for order processing and intermittently produces wrong results. Why
and how do you fix it?
7. ■ An API works fine in testing but throws ConcurrentModificationException randomly in production. What
causes this and how do you fix it?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 16
7. ■ A refactoring accidentally removed @Transactional from a service method. How do you detect this in code
review and in integration tests?
Distributed Systems
1. ■ A payment microservice calls inventory and notification services. If inventory fails, how do you rollback
payment using the Saga pattern?
2. ■ Service A calls B, which calls C. A timeout in C causes all threads in A and B to be exhausted. What
pattern solves this?
3. ■ A client calls your payment API multiple times due to network retries, causing duplicate charges. How do
you prevent this?
4. ■ Your event-driven service publishes an event but the consumer fails and the event is lost. How do you
ensure at-least-once delivery?
5. ■ A Kafka consumer processes the same event twice due to a retry. How do you ensure idempotency in
the handler?
6. ■ How do you debug a slow request that crosses 5 microservices with no centralized log?
7. ■ Your circuit breaker is opening too aggressively on transient errors. How do you tune it to distinguish transient
from permanent failures?
8. ■ A new deployment breaks an existing Kafka consumer because the event schema changed. How do you handle
schema evolution?
9. ■ Your Redis cache is down. How does your Spring Boot application behave and how do you configure graceful
degradation?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 17
SECTION 12 — TRICKY & COMMONLY FAILED QUESTIONS
Note: These questions have the highest failure rate. They test true understanding vs surface memorization.
1. ■ What is the output when a finally block has a return statement that conflicts with a return in the try
block?
2. ■ You put a mutable object as a HashMap key and then mutate the object. What happens when you call
get() with the same reference?
3. ■ String s1='Java'; String s2=new String('Java'); String s3=[Link](); — which == comparisons return true
and why?
4. ■ Can a constructor throw an exception? If so, is the object created on the heap?
5. ■ A @Transactional method calls another @Transactional method in the same class. Does the inner
transaction start a new one?
6. ■ You annotate a private method with @Transactional. Does it work? Why or why not?
7. ■ A Spring singleton bean has an instance variable modified per-request. Is this thread-safe?
8. ■ HashMap has load factor 0.75 and initial capacity 16. After exactly how many entries does it resize?
9. ■ What is the output: List list=new ArrayList(); [Link](1); [Link](2); for(Integer
i:list){if(i==1)[Link](i);} — and why?
10. ■ What is the risk of [Link]() without isPresent()? What is the safe alternative?
11. ■ How do you distinguish [Link]() returning null because the key is absent vs because the key
maps to a null value?
12. ■ Does adding @Async to a method guarantee it runs in a separate thread if the caller is in the same
class?
13. ■ You use parallelStream().forEach() to write to a non-thread-safe ArrayList. What happens and what is
the correct fix?
14. ■ You call notify() but no thread is waiting. What happens? Is the signal lost?
15. ■ [NEW] What happens when you call a @Transactional method on a bean you instantiated directly (new
MyService()) instead of injecting it?
16. ■ [NEW] What exception is thrown to the caller when you call [Link]() and the async task
throws a RuntimeException?
17. ■ Can you make a static field volatile? What does it achieve?
18. ■ [NEW] What happens when two threads call [Link]() concurrently in Java 8+? Can it cause an infinite
loop unlike Java 7?
19. ❄ [NEW] What is the difference between a + b and a += b when a and b are declared as byte in terms of overflow
and casting?
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 18
QUICK REFERENCE — TOP 30 MUST-KNOW QUESTIONS
# Question Topic
2 N+1 query problem in JPA: how to detect and all ways to fix it.
17 CQRS and Event Sourcing: benefits, drawbacks, and when to use them.
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 19
JAVA BACKEND DEVELOPER
INTERVIEW QUESTION BANK
Enhanced Edition | 380+ Questions | India Market
12 Sections: Core Java | JVM Internals | Collections | Multithreading | Java 8+ | Spring Boot |
Spring Testing | REST API | JPA/Hibernate | SQL | Microservices | Debugging
Java Backend Developer Interview Question Bank | Fresher to 3 Years | India Market Page 20