Java developer interview questions, and how to answer them

Java interviews test whether you understand what your code does once it's running, not whether you've memorised the Collections API. Expect questions about Spring, the database layer, threads and memory, plus a coding round and a conversation about a production problem you've fixed. Here's what each question is really checking.

The process

What happens in each round

  1. 1

    Recruiter screen

    What happens

    Whether your stack matches the posting. They'll ask which Java version you've worked with, whether you've used Spring Boot in production, and which database and cloud you know. Name them plainly.

  2. 2

    Technical phone screen

    What happens

    Core Java fundamentals in conversation: equals and hashCode, immutability, exceptions, collections, and a short coding problem in a shared editor like CoderPad. They're listening for how you reason out loud.

  3. 3

    Coding round

    What happens

    Clean, working Java under time pressure. Some companies use algorithm puzzles, many Java shops prefer a practical task like building a small REST endpoint or refactoring a messy class, and they watch whether you write tests.

  4. 4

    System design or architecture

    What happens

    How you'd build a service: API shape, data model, caching, messaging with Kafka or a queue, and what happens when a downstream system is slow. More weight for senior roles.

  5. 5

    Team and hiring manager conversation

    What happens

    How you handle code review, on-call, disagreement and legacy code. They want someone who can work in an old codebase without complaining about it all day.

Questions you're likely to get

1.What happens if you override equals but not hashCode?

Why they ask

It's the classic check on whether you understand how HashMap and HashSet actually find objects, which causes real bugs in entity classes.

How to answer

  • State the contract: equal objects must return the same hashCode
  • Explain that HashMap uses hashCode to pick a bucket before calling equals
  • Give the symptom: you put an object in a HashSet and contains returns false for an equal copy
  • Mention the JPA angle: be careful basing equals on a generated ID that's null before save
2.How does Spring's dependency injection work, and why would you prefer constructor injection?

Why they ask

Almost every Java job runs on Spring, and they want to know you understand the container rather than sprinkling annotations until it starts.

How to answer

  • The application context scans for components and creates beans, wiring their dependencies
  • Constructor injection makes dependencies explicit and lets fields be final
  • It makes unit tests easy because you can pass in mocks without starting Spring
  • It surfaces circular dependencies at startup instead of hiding them
3.What's the N plus one query problem in Hibernate, and how do you fix it?

Why they ask

It's the most common performance bug in JPA code, and anyone who's run a Spring Data service in production has hit it.

How to answer

  • Loading a list of parents triggers a separate query for each child collection
  • You spot it by turning on SQL logging or seeing a slow endpoint in an APM tool
  • Fix with a fetch join, an entity graph, or batch fetching
  • Mention that switching everything to eager loading usually makes it worse
4.Explain how @Transactional works and a case where it silently doesn't.

Why they ask

Transaction bugs lose data, and Spring's proxy model trips up a lot of mid-level developers.

How to answer

  • Spring wraps the bean in a proxy that opens and commits the transaction around the method call
  • A call from one method to another inside the same class skips the proxy, so the annotation does nothing
  • By default only unchecked exceptions trigger a rollback
  • Talk about propagation briefly, for example REQUIRES_NEW for audit logging
5.What's the difference between synchronized, a ReentrantLock and a ConcurrentHashMap?

Why they ask

They want to know whether you can write thread-safe code in a web service where many requests hit shared state at once.

How to answer

  • synchronized locks on a monitor and is simple but inflexible
  • ReentrantLock adds tryLock, timeouts and fairness when you need them
  • ConcurrentHashMap gives safe concurrent access without locking the whole map
  • Say that the best fix is often to avoid shared mutable state entirely
6.A service is throwing OutOfMemoryError in production. How do you investigate?

Why they ask

This is the JVM question that separates people who've been on call from people who haven't.

How to answer

  • Check which memory area ran out: heap, metaspace, or direct memory
  • Capture a heap dump and open it in Eclipse MAT or VisualVM to find what's holding references
  • Look for the usual suspects: an unbounded cache, a static collection, a huge result set loaded into memory
  • Fix the leak first, then decide whether heap settings need changing
7.When would you use a Stream and when would you stick with a loop?

Why they ask

They're checking taste. Streams can make code clearer or much harder to read and debug.

How to answer

  • Streams read well for filter, map and collect pipelines
  • Loops are clearer when there's early exit, checked exceptions or complex state
  • Parallel streams use the common fork-join pool and rarely help in a web service
  • Show you'd pick whichever the next reader will understand fastest
8.How would you design a REST API for placing and tracking orders?

Why they ask

It's a practical design question that shows how you think about resources, errors and failure between services.

How to answer

  • Sketch resources and verbs: POST to create, GET to track, sensible status codes
  • Handle retries with an idempotency key so a double submit doesn't create two orders
  • Validate input with Bean Validation and return consistent error bodies
  • Discuss publishing an event to Kafka for downstream systems instead of calling each one directly
9.How do you test a Spring Boot service?

Why they ask

Teams want developers who ship tested code, and Java has well-established tools for it.

How to answer

  • Plain JUnit and Mockito tests for business logic with no Spring context
  • Slice tests like @WebMvcTest or @DataJpaTest for the web and database layers
  • Testcontainers for integration tests against a real database or Kafka
  • Keep full @SpringBootTest runs few because they're slow
10.Tell me about a production incident you helped fix.

Why they ask

They want evidence you've owned real systems and can stay calm and methodical under pressure.

How to answer

  • Set the scene briefly: what broke and who noticed
  • Walk through how you narrowed it down using logs, metrics or a thread dump
  • Say what the fix was and what you changed so it wouldn't happen again
  • Own your part honestly, including anything you'd do differently
11.How do you approach changing legacy code that has no tests?

Why they ask

Most Java roles involve old code, and they need someone who improves it carefully rather than rewriting it.

How to answer

  • Write characterisation tests that capture current behaviour before touching anything
  • Make small refactors in separate commits so review is easy
  • Leave the area a bit cleaner each time instead of proposing a full rewrite
  • Mention agreeing the approach with the team before large changes
12.Tell me about a code review where you disagreed with someone.

Why they ask

Code review is where Java teams spend a lot of time, and friction there slows everyone.

How to answer

  • Describe the specific technical point, not just the personality clash
  • Explain how you backed your view with evidence, a benchmark or a doc
  • Show you were willing to be wrong or to compromise
  • Say how it ended and what you learned

Mistakes that sink good candidates

Reciting textbook definitions of OOP principles without tying them to code you've written

Claiming microservices experience you can't describe in detail when they ask how services talked to each other

Complaining about legacy code at a previous job, since you'll probably be maintaining theirs

Going silent in the coding round instead of talking through your approach

Need more Java Developer interviews to prep for?

HeroApply applies to Java Developer jobs that match you, every day. 2,392 jobs are open today.

Find Java Developer jobs