Backend interview preparation

Java Interview Questions

Prepare for Java backend interviews with core language, collections, JVM, concurrency, Spring and persistence questions.

8 topics · 40 interview questions

0%0 / 40 reviewed

Revision checklist saved in this browser. It is separate from your account’s solved-question count.

Java language and object model

0 / 5

Use short examples to explain contracts, references and object state.

Official topic reference ↗
  • Is Java pass-by-value or pass-by-reference?
    Answer checkpoints

    Explain that a method receives a copy of the argument value, including a copied reference. Show the difference between changing an object and reassigning the parameter.

  • How do equals and hashCode work together?
    Answer checkpoints

    Describe equality properties and equal objects having equal hash codes. Discuss mutable keys in hash-based collections.

  • When should you use interfaces, abstract classes or composition?
    Answer checkpoints

    Compare contracts, shared state and implementation reuse. Explain how composition keeps responsibilities replaceable.

  • What makes an object immutable?
    Answer checkpoints

    Discuss final fields, construction safety and defensive copies. A final reference does not make the referenced collection immutable.

  • How do checked and unchecked exceptions differ?
    Answer checkpoints

    Explain declaration and handling requirements. Choose an exception strategy based on recoverability and the API contract.

Collections and generics

0 / 5

Choose by workload and explain the cost of each operation.

Official topic reference ↗
  • How do ArrayList and LinkedList differ?
    Answer checkpoints

    Compare indexed access, traversal and insertion when the position is already known. Include allocation and cache-locality costs.

  • How do HashMap and TreeMap differ?
    Answer checkpoints

    Compare key ordering, comparison rules and lookup complexity. Explain when a sorted map is useful.

  • Why are generic types invariant?
    Answer checkpoints

    Use a collection example to show why a List of a subtype cannot safely substitute for every List of its parent type.

  • How do extends and super wildcards guide API design?
    Answer checkpoints

    Explain reading from a producer and writing to a consumer. Show a copy operation with constrained source and destination types.

  • What is the difference between an unmodifiable view and a defensive copy?
    Answer checkpoints

    Explain whether later mutations in an underlying collection become visible. Distinguish shallow copying from deep immutability.

Streams and functional style

0 / 5

Explain execution and side effects before comparing syntax.

Official topic reference ↗
  • How do intermediate and terminal stream operations differ?
    Answer checkpoints

    Explain laziness and when traversal begins. Describe why a consumed stream cannot simply be reused.

  • When would you use map versus flatMap?
    Answer checkpoints

    Compare transforming one value with flattening nested results. Use an example involving optional or collection-valued results.

  • Why can side effects make a stream pipeline fragile?
    Answer checkpoints

    Discuss shared mutable state, execution order and parallel execution. Prefer explicit accumulation or a suitable collector.

  • When is a parallel stream appropriate?
    Answer checkpoints

    Consider data size, splitting cost, CPU work and the shared execution environment. Measure rather than assuming parallel is faster.

  • How should Optional be used at API boundaries?
    Answer checkpoints

    Use it to express an absent result where appropriate. Explain eager versus lazy fallback computation and avoid unchecked get calls.

JVM and memory

0 / 5

Distinguish language guarantees from runtime implementation choices.

Official topic reference ↗
  • How do stack frames, heap objects and GC roots relate?
    Answer checkpoints

    Describe method execution and object reachability. Avoid claiming that every implementation must allocate every object identically.

  • Can a Java application have a memory leak?
    Answer checkpoints

    Explain unintended retention through static collections, listeners or thread-local values. Identify the reference chain keeping objects reachable.

  • What causes long garbage-collection pauses?
    Answer checkpoints

    Discuss allocation rate, live-set size, heap configuration and collector choice. Use logs and a workload profile before tuning.

  • How would you investigate high CPU in a Java service?
    Answer checkpoints

    Correlate thread activity with stack samples and application traces. Distinguish busy application work, contention and garbage collection.

  • What is the role of JIT compilation?
    Answer checkpoints

    Explain runtime profiling and optimized compilation. Describe warmup effects and why a naive timing loop can mislead.

Concurrency

0 / 5

Reason about visibility, atomicity and coordination separately.

Official topic reference ↗
  • How do synchronized and volatile differ?
    Answer checkpoints

    Explain mutual exclusion and visibility guarantees. A volatile counter increment is still a compound operation.

  • What does a happens-before relationship establish?
    Answer checkpoints

    Describe the ordering and visibility guarantees used to reason about shared state. Use a lock or safe-publication example.

  • How do you choose an executor and bound its work queue?
    Answer checkpoints

    Connect pool size and queue capacity to CPU or I/O workloads. Include overload rejection, shutdown and cancellation.

  • When are virtual threads useful?
    Answer checkpoints

    Discuss blocking I/O concurrency and why virtual threads do not make CPU-bound work faster. Preserve limits on scarce downstream resources.

  • How can CompletableFuture error handling go wrong?
    Answer checkpoints

    Explain composition, exceptional completion and explicit executor choices. Avoid silently swallowing failures or blocking dependent work.

Spring and web APIs

0 / 5

Explain how framework mechanisms affect request behavior and object ownership.

Official topic reference ↗
  • What does dependency injection change in service design?
    Answer checkpoints

    Discuss constructor injection, explicit dependencies and replaceable boundaries. Explain that injection alone does not guarantee good separation of responsibilities.

  • What does singleton scope mean for a Spring bean?
    Answer checkpoints

    Explain the container-scoped shared instance and why mutable state must be handled safely across requests.

  • How does proxy-based transaction interception work?
    Answer checkpoints

    Discuss which calls pass through the proxy. Explain why an internal self-invocation may bypass transactional advice.

  • How should a REST API validate input and return errors?
    Answer checkpoints

    Validate at boundaries, choose suitable status codes and maintain a consistent error contract. Avoid exposing internal details.

  • Where should authentication and authorization checks live?
    Answer checkpoints

    Cover endpoint and service-level permission checks. Treat CORS, identity verification and access decisions as separate concerns.

Persistence and transactions

0 / 5

Translate ORM behavior into SQL and transaction boundaries.

Official topic reference ↗
  • What causes N+1 queries in an ORM?
    Answer checkpoints

    Trace the repeated queries during relationship access. Compare projections, fetch joins and batched loading for the specific use case.

  • How does optimistic locking prevent lost updates?
    Answer checkpoints

    Explain a version check and conflict detection. Describe when to retry and when the caller must resolve the conflict.

  • How do isolation levels affect application behavior?
    Answer checkpoints

    Discuss anomalies relevant to the invariant being protected. Include the concurrency cost of stronger isolation.

  • How would you make database pagination stable?
    Answer checkpoints

    Use a deterministic sort and a tie-breaker. Compare large offsets with a keyset cursor and appropriate indexes.

  • How do transactions interact with message publishing?
    Answer checkpoints

    Explain the failure window between a commit and sending a message. Describe an outbox and idempotent consumers.

Practical design exercises

0 / 5

Explain failure behavior and how you would verify the design.

Official topic reference ↗
  • Design a bounded producer-consumer pipeline.
    Answer checkpoints

    Choose queue capacity, blocking or rejection behavior and a shutdown protocol. Explain how failures propagate to producers and consumers.

  • Design a cache for a frequently read endpoint.
    Answer checkpoints

    Define cache keys, expiry and invalidation. Discuss stampede protection and whether stale data is acceptable.

  • Design an idempotent payment request handler.
    Answer checkpoints

    Persist an idempotency key and outcome with appropriate transactional protection. Handle concurrent duplicates and unknown external outcomes.

  • Find and fix a deadlock in a service.
    Answer checkpoints

    Explain lock ordering, waiting dependencies and how a thread dump supports the diagnosis. Reduce lock scope or remove cyclic acquisition.

  • Plan a backward-compatible API change.
    Answer checkpoints

    Cover consumers, schema evolution, deployment order and observability. Define how old and new application versions coexist.