Skip to main content
Skip to main content
Tutorsbot
java

Top 25 Java Interview Questions for 2026 (with Answers)

25 Java interview questions covering JVM, garbage collection, concurrency, generics, streams, records, sealed classes, and virtual threads. With structured answers.

August 19, 20262 min read

Java interviews test depth: the JVM, the garbage collector, the memory model, and modern language features. These 25 questions cover what interviewers actually ask in 2026 — including JDK 21 features (records, sealed classes, virtual threads).

JVM & memory (Q1–Q8)

  • What is the JVM? The Java Virtual Machine — executes bytecode, manages memory, JIT, GC, threads.
  • What is the JIT? Just-in-time compiler compiles hot bytecode paths to native code at runtime.
  • What is the GC? Garbage collection — automatic memory management. G1 is default since JDK 9; ZGC and Shenandoah are low-pause.
  • Stack vs heap — stack holds local references + primitives; heap holds objects.
  • What is the JMM? Java Memory Model — defines happens-before. `volatile`, `synchronized`, `final` guarantee visibility.
  • What are the classloaders? Bootstrap → Platform → Application. Custom loaders for OSGi / hot reload.
  • What is bytecode? Intermediate .class format. `javap` disassembles; ASM manipulates.
  • What is the metaspace? Native memory for class metadata (replaced PermGen in Java 8).

Language features (Q9–Q17)

  • `==` vs `.equals()` — `==` compares references; `.equals()` compares contents.
  • Generics + erasure — type parameters are erased at runtime; `List` and `List` share bytecode.
  • PECS — Producer Extends Consumer Super. Use `? extends T` for read, `? super T` for write.
  • `Optional` — represents a value-or-absent. Avoid as a method parameter.
  • Streams — lazy intermediate ops + eager terminal ops. Use parallel streams carefully.
  • Records — `record Point(int x, int y)` — auto-generates accessors, equals, hashCode, toString.
  • Sealed classes — `sealed interface Shape permits Circle, Square`. Enables exhaustive switch.
  • Pattern matching switch — `case Integer i when i > 0 -> ...`. JDK 21.
  • Virtual threads — JDK 21 lightweight threads (millions per JVM). Eliminates thread-pool sizing for I/O.

Concurrency (Q18–Q25)

  • `synchronized` vs `ReentrantLock` — `ReentrantLock` supports timed waits, interruptible locks, condition variables.
  • `volatile` guarantees — visibility + prevents instruction reordering. Not atomic.
  • `ConcurrentHashMap` — fine-grained locking; allows concurrent reads + structured concurrent writes.
  • `CompletableFuture` — composable async. `thenApply`, `thenCompose`, `thenCombine`, `allOf`.
  • Thread pools — `ExecutorService`, `ForkJoinPool`, structured concurrency (preview in JDK 21+).
  • Deadlock detection — `jstack`, `jcmd`, thread dumps, ThreadMXBean.
  • Structured concurrency — treat related threads as a unit; failure in one cancels the others.
  • Atomic classes — `AtomicInteger`, `AtomicReference`. Lock-free CAS operations.

Further reading

Authoritative sources

Related Tutorsbot tutorials

Share:

Related Articles