# java & jvm
// [35] real interview questions. Answers and sources live in the practice app.
practice this topic- Explain heap vs stack memory in the JVM. What lives where, and what errors signal exhaustion of each?(junior)
- How does garbage collection work in the JVM at a conceptual level?(mid)
- Your service has strict p99 latency targets and a 30 GB heap. How do you think about choosing a garbage collector?(senior)
- State the equals/hashCode contract. What concretely breaks if you override equals but not hashCode?(mid)
- Checked vs unchecked exceptions: what is the difference, and why are checked exceptions controversial?(mid)
- What is the difference between int and Integer, and what pitfalls does autoboxing introduce?(junior)
- What does volatile guarantee, and when is it not enough (vs synchronized)?(mid)
- Compare synchronized blocks with ReentrantLock. When would you deliberately choose the lock?(senior)
- What are virtual threads, and when do they help — and not help?(senior)
- You migrate a service to virtual threads. What operational pitfalls should you check for?(senior)
- In the Stream API, what does it mean that intermediate operations are lazy?(mid)
- What are the classic pitfalls of the Stream API in production code?(senior)
- A colleague adds .parallel() to every stream 'for speed'. When does a parallel stream actually help, and when does it hurt?(senior)
- Walk through the JVM's runtime memory areas beyond heap and stack. Where do classes, constants, and JIT-compiled code live?(mid)
- How do you design an immutable class in Java, and why are immutable objects valuable in concurrent code?(mid)
- 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)
- p99 latency spikes on your service line up exactly with G1 GC pauses. Describe your tuning process — not just flags.(senior)
- When do records fit, when do they not, and where does validation go in a record?(mid)
- What design problem do sealed classes solve, and how do they combine with records and switch pattern matching?(mid)
- Constructor injection vs field injection in Spring: why does the Spring team recommend constructors, and what happens with circular dependencies?(mid)
- A @Transactional method calls another @Transactional(REQUIRES_NEW) method on the same class, but no new transaction ever starts. Why?(senior)
- After several hot redeploys, your app server dies with a Metaspace OutOfMemoryError. Connect that to classloaders and ThreadLocals.(senior)
- What are ScopedValues, and which ThreadLocal problems were they designed to fix?(mid)
- Production throws java.lang.OutOfMemoryError: Java heap space every few days. Give your diagnosis workflow, tools included.(senior)
- 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)
- 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)
- When would you deliberately use a StringBuilder over string concatenation in Java, given the compiler already optimizes '+'? (mid)
- 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)
- 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)
- Why is Optional a return type and (mostly) not a field or parameter type? What is the idiomatic way to use it?(mid)
- What does type erasure mean for Java generics, and which real limitations does it impose?(mid)
- 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)
- How do you correctly size and configure a ThreadPoolExecutor, and what are the queue/rejection choices that bite in production?(mid)
- 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)
- Why should try-with-resources be preferred over manual close in a finally block, and what subtle bug does it fix?(mid)