Skip to content
Preptima

Java & Spring interview questions and answers

Language and framework depth for JVM roles, covering the internals that separate framework users from engineers who understand what runs underneath.

41 published across 10 topics.

Core Java

3 questions

Object semantics, equals and hashCode contracts, generics, exceptions, and immutability.

mediumConceptCoding

If you override equals but not hashCode, what breaks and when?

Equal objects must produce equal hash codes. Break that and a HashMap sends lookups to a bucket the entry was never placed in, so the map holds a value you can no longer retrieve and a set silently admits duplicates. The same failure appears later if a key is mutated after insertion.

4 minentry, mid, senior
All 3 in Core Java

Implementation internals, resizing behaviour, ordering guarantees, and choosing the right collection.

mediumConceptCoding

Sorting a list throws IllegalArgumentException saying the comparison method violates its general contract. What did the comparator get wrong?

The sort detected that your comparator contradicted itself partway through a merge. The usual causes are a subtraction that overflows, an ordering that is not transitive, and a sort key that changes while the sort is running. Use this collections answer to show the decision, trade-off, and evidence rather than a memorised definition. It also connects sorting to the point an interviewer is testing.

5 minmid, senior, staff
mediumConcept

How does HashMap work internally, and what happens when it resizes?

HashMap stores entries in an array of buckets indexed by a hash of the key; collisions chain into a linked list that converts to a red-black tree past a threshold, and exceeding the load factor triggers a resize that reallocates and redistributes every entry. It also connects collections to the point an interviewer is testing.

3 minentry, mid, senior
All 4 in Collections Framework

The memory model, executors, locks, atomics, virtual threads, and diagnosing races and deadlocks.

hardCodingDesign

Implement a thread-safe pool of N connections with acquire, release and a timeout.

A thread-safe connection pool pairs a Semaphore of N permits, which bounds how many connections exist and gives acquire its timeout, with a queue of idle connections. Acquire returns a lease that releases exactly once from a finally block, and a broken connection is discarded without losing its permit.

6 minmid, senior
mediumConceptDesign

What problem do virtual threads actually solve, and when do they not help?

Virtual threads make blocking cheap by unmounting from their carrier OS thread whenever they block, so thread-per-request scales to hundreds of thousands of concurrent tasks. Finalised in JDK 21, they do nothing for CPU-bound work, and pooling them defeats their purpose.

5 minmid, senior, staff
hardConceptCoding

What does volatile guarantee, and what does it not?

It guarantees visibility and ordering: a volatile write happens-before every later read of that field, so everything the writer did beforehand becomes visible to the reader. It guarantees nothing about atomicity, so an increment is still a race and a volatile reference says nothing about the object it points to.

5 minmid, senior, staff
All 4 in Concurrency & Multithreading

Class loading, memory regions, garbage collectors, and reading a heap dump or GC log.

hardScenarioConcept

A service dies with OutOfMemoryError in production. Walk me through diagnosing it.

Read the message text first, because it names which region ran out. Then use the GC log to see whether the live set after each full collection is trending upward, which means a leak, or flat and close to the ceiling, which means the heap is simply too small. A heap dump tells you what is retaining the memory.

6 minmid, senior, staff
All 5 in JVM Internals & Tuning

Lazy evaluation, collectors, parallel streams, and the readability and performance traps.

mediumConceptCoding

Streams are lazy until a terminal operation runs. Why does that matter, and when would you make one parallel?

Laziness means intermediate operations only describe a pipeline, so nothing traverses the source until a terminal operation pulls, and short-circuiting operations can stop early. Parallel helps when the source splits evenly, the work per element is CPU-bound and substantial. Use this streams answer to show the decision, trade-off, and evidence rather than a memorised definition.

5 minmid, senior
All 4 in Streams & Functional Java

Records, sealed types, pattern matching, and the language changes that reshape idiomatic code.

mediumConceptDesign

How do records, sealed types and pattern matching change the way you model a domain?

Together they let you write closed data hierarchies the compiler can check. A record declares that a type is its components, a sealed interface enumerates the permitted variants, and an exhaustive switch over record patterns turns adding a variant into a compile error at every place that handles them.

5 minmid, senior, staff
All 3 in Modern Java Features

Spring Core

6 questions

Dependency injection, bean lifecycle, scopes, proxies, and why an AOP annotation silently does nothing.

hardConceptScenario

A method with REQUIRES_NEW was meant to save an audit row even when the main transaction fails. When does it work, and when does it deadlock?

REQUIRES_NEW suspends the caller's transaction and commits the audit on a second connection, so the row survives a later rollback if the call goes through the Spring proxy. It hangs when the audit touches rows the suspended transaction has locked, or when every thread holds one pooled connection and waits for another.

5 minmid, senior, staff
mediumConceptScenario

Startup fails with NoUniqueBeanDefinitionException after someone adds a second implementation. Do you reach for @Primary, @Qualifier or a list of beans, and which fix ages badly?

NoUniqueBeanDefinitionException means one injection point matches two beans by type. Fix it where the decision belongs: a qualifier at the consumer when callers need different beans, a List or Map when all are used, a conditional bean when only one should exist. A primary bean is the quick fix that ages worst.

4 minmid, senior
mediumConceptScenario

Why does @Transactional sometimes do nothing at all, even though the annotation is right there?

Spring applies @Transactional through a proxy that wraps the bean. Calling the annotated method from another method of the same class goes through `this`, not the proxy, so the advice never runs and no transaction is started. The same silence affects @Cacheable, @Async and @Retryable. It also connects dependency injection to the point an interviewer is testing.

4 minmid, senior
All 6 in Spring Core

Spring Boot

5 questions

Auto-configuration mechanics, configuration precedence, actuator, and production-readiness concerns.

mediumScenarioConcept

A property has one value in application.yml, another in production, and nobody can say where the production value comes from. How do you find out?

Ask the running application, not the files. Actuator's env endpoint queried for that property lists the property sources in precedence order with the value each holds, the first holder winning. Then trace it: an environment variable matched by relaxed binding, a profile file, a command-line argument or a JVM system property.

4 minmid, senior
mediumConceptCoding

Why would you bind configuration with @ConfigurationProperties rather than @Value, and how do you make a bad value fail at startup instead of at the first request?

@Value injects one expression per field with no grouping, no relaxed name matching and no validation. @ConfigurationProperties binds a whole subtree onto a typed object, matches names loosely, handles lists and nested types, and combined with @Validated it fails the context refresh when the environment is wrong.

4 minmid, senior, staff
All 5 in Spring Boot

Entity lifecycle, lazy loading and N+1, transaction propagation, and generated-SQL debugging.

hardScenarioConcept

A Spring Data repository method looks like one clean line, but the endpoint issues several hundred queries per request. How do you diagnose the N+1, and what would you actually change?

The repository call fetches parents in one query; the extra queries come later, when something touches a lazy association - usually the JSON serialiser, with open-session-in-view keeping the context alive so it succeeds silently. Diagnose by counting statements per request, then fix with a join fetch or entity graph, batch fetching, or a DTO projection.

4 minmid, senior, staff
mediumConceptScenario

Nobody called save, but the UPDATE still went to the database. Explain how that happened.

Hibernate dirty checking tracks a managed JPA entity through the persistence context. At flush the provider compares the entity against its loaded snapshot and writes an UPDATE for whatever differs, so mutating a managed object inside a transaction is itself the instruction to persist. Use this spring data jpa answer to show the decision, trade-off, and evidence rather than a memorised definition.

5 minmid, senior, staff
All 4 in Spring Data & JPA

Unit versus integration boundaries, mocking strategy, Testcontainers, and suite design.

All 3 in Testing on the JVM