# java & jvm

// [35] real interview questions. Answers and sources live in the practice app.

practice this topic
  1. Explain heap vs stack memory in the JVM. What lives where, and what errors signal exhaustion of each?(junior)
  2. How does garbage collection work in the JVM at a conceptual level?(mid)
  3. Your service has strict p99 latency targets and a 30 GB heap. How do you think about choosing a garbage collector?(senior)
  4. State the equals/hashCode contract. What concretely breaks if you override equals but not hashCode?(mid)
  5. Checked vs unchecked exceptions: what is the difference, and why are checked exceptions controversial?(mid)
  6. What is the difference between int and Integer, and what pitfalls does autoboxing introduce?(junior)
  7. What does volatile guarantee, and when is it not enough (vs synchronized)?(mid)
  8. Compare synchronized blocks with ReentrantLock. When would you deliberately choose the lock?(senior)
  9. What are virtual threads, and when do they help — and not help?(senior)
  10. You migrate a service to virtual threads. What operational pitfalls should you check for?(senior)
  11. In the Stream API, what does it mean that intermediate operations are lazy?(mid)
  12. What are the classic pitfalls of the Stream API in production code?(senior)
  13. A colleague adds .parallel() to every stream 'for speed'. When does a parallel stream actually help, and when does it hurt?(senior)
  14. Walk through the JVM's runtime memory areas beyond heap and stack. Where do classes, constants, and JIT-compiled code live?(mid)
  15. How do you design an immutable class in Java, and why are immutable objects valuable in concurrent code?(mid)
  16. Your team debates rewriting a blocking Spring MVC service as reactive WebFlux 'for scalability'. Java 21 is available. How do you frame the decision?(senior)
  17. p99 latency spikes on your service line up exactly with G1 GC pauses. Describe your tuning process — not just flags.(senior)
  18. When do records fit, when do they not, and where does validation go in a record?(mid)
  19. What design problem do sealed classes solve, and how do they combine with records and switch pattern matching?(mid)
  20. Constructor injection vs field injection in Spring: why does the Spring team recommend constructors, and what happens with circular dependencies?(mid)
  21. A @Transactional method calls another @Transactional(REQUIRES_NEW) method on the same class, but no new transaction ever starts. Why?(senior)
  22. After several hot redeploys, your app server dies with a Metaspace OutOfMemoryError. Connect that to classloaders and ThreadLocals.(senior)
  23. What are ScopedValues, and which ThreadLocal problems were they designed to fix?(mid)
  24. Production throws java.lang.OutOfMemoryError: Java heap space every few days. Give your diagnosis workflow, tools included.(senior)
  25. You fan out to three backends for one request and need: cancel the rest when one fails, and never leak a runaway subtask. What does structured concurrency offer over raw futures?(senior)
  26. A latency-sensitive service is fast in steady state but its first thousand requests after each deploy are painfully slow. Explain the JIT warmup effect and your options.(senior)
  27. When would you deliberately use a StringBuilder over string concatenation in Java, given the compiler already optimizes '+'? (mid)
  28. A shared counter guarded by synchronized shows up as a hot lock under contention. When do AtomicInteger, LongAdder, or a plain lock each win?(mid)
  29. Two threads acquiring two locks in different orders occasionally freezes your service. How do you diagnose a deadlock and prevent the class of bug?(senior)
  30. Why is Optional a return type and (mostly) not a field or parameter type? What is the idiomatic way to use it?(mid)
  31. What does type erasure mean for Java generics, and which real limitations does it impose?(mid)
  32. A Spring bean holds request-specific data in a field and occasionally serves one user's data to another. What is the bean-scope bug and how do you fix it?(senior)
  33. How do you correctly size and configure a ThreadPoolExecutor, and what are the queue/rejection choices that bite in production?(mid)
  34. Your service's biggest cost is object allocation churn in a hot path. How do you reason about the JVM allocation and escape-analysis story before reaching for object pools?(senior)
  35. Why should try-with-resources be preferred over manual close in a finally block, and what subtle bug does it fix?(mid)