Java Core Senior interview questions and answers
75 questions for a Java Core (Senior) interview - with correct answers, explanations and links to the docs.
- 1.Why does double-checked locking for a lazy singleton need a
volatilefield? - 2.What does 'happens-before' mean in the Java Memory Model?
- 3.Why are properly built immutable objects safe to share between threads?
- 4.What's wrong with this cache code under concurrency?
- 5.Which thread runs
CompletableFuture.supplyAsync(task)without an executor? - 6.An exception is thrown inside
thenApply. What does the caller see onjoin()? - 7.When do virtual threads (JEP 444) help most?
- 8.What can 'pin' a virtual thread to its carrier in Java 21?
- 9.Why are
ThreadLocals a concern with millions of virtual threads? - 10.What does structured concurrency (JEP 453) aim to fix?
- 11.Which garbage collector is the default in modern JDKs on servers?
- 12.When would you pick ZGC over G1?
- 13.Why should
-XmxorMaxRAMPercentagebe set for a JVM in a container? - 14.Test runs grow slower and finally fail with
OutOfMemoryError: Metaspace. What's a common cause? - 15.What are GC logs useful for when tests or services pause randomly?
- 16.Why are naive microbenchmarks in Java often wrong?
- 17.In a JMH benchmark, why consume results with a
Blackholeor return them? - 18.What does
junit.jupiter.execution.parallel.enabled=truechange? - 19.What does
@ResourceLock("DB")do in JUnit 5? - 20.With TestNG
parallel="methods", why do tests sharing instance fields break? - 21.Why use Testcontainers in Java integration tests?
- 22.What's the main risk of over-mocking in Java unit tests?
- 23.Why should JPA entity
equalsnot rely on a generated id alone? - 24.Why did many libraries break when moving from Java 8 to 17?
- 25.What is heap pollution with generic varargs?
- 26.Why does
Collectors.toMap(...)throwIllegalStateExceptionon some data? - 27.Why is logging with
log.debug("Order " + order)wasteful? - 28.Why does logging context (MDC) disappear in tasks on a thread pool?
- 29.What does a sealed interface plus records give for domain modeling?
- 30.Why may a
switchwith patterns fail to compile with 'dominated' cases? - 31.What's the risk of
stream().parallel()on aLinkedListor with blocking calls? - 32.Why can autoboxing hurt performance in hot code?
- 33.What does a thread dump help with in a hanging test run?
- 34.Why should a test framework's driver or client factory be thread-safe?
- 35.What problem does a test data builder solve?
- 36.Why is
catch (InterruptedException e) {}a bug? - 37.Why does
ExecutorService.shutdown()matter in tests? - 38.What happens when you call
hashCodeon a mutable record used as a map key? - 39.Why prefer composition over inheritance in a large test framework?
- 40.What do JUnit 5 extensions (
@ExtendWith) offer over base classes? - 41.Why should exception messages include context in a test framework?
- 42.What's the issue with a static
SimpleDateFormatshared by threads? - 43.How do you make code that reads
System.getenvtestable? - 44.Why can
equalsthat usesinstanceofbreak symmetry with subclasses? - 45.When is
ReentrantLocka better choice thansynchronized? - 46.Why may a
ConcurrentHashMap.computeIfAbsentthat calls itself deadlock or fail? - 47.What does
ExecutorService.invokeAllwith a timeout give you? - 48.Why is a flaky test that passes on retry still a problem in Java suites?
- 49.What is a common cause of tests that fail only on CI machines with more cores?
- 50.Why is
Thread.stop()gone? - 51.What does
Stream.toList()return compared tocollect(Collectors.toList())? - 52.Why may a Java service using a JDBC pool hang after a database failover?
- 53.What's the downside of catching
Throwablein a test listener? - 54.Why prefer
List.copyOfoverCollections.unmodifiableListfor API return values? - 55.What does
jcmd <pid> GC.heap_dumpgive you? - 56.Why can a Java test pass in the IDE but fail with Maven?
- 57.Why should library code avoid
System.exit? - 58.What's a safe way to publish an object to other threads?
- 59.Why does the JIT matter for performance tests of a Java service?
- 60.What risk do
@Disabledorenabled=falsetests carry over time? - 61.Why are records a good fit for API test DTOs?
- 62.What does
Optional.ofNullable(x).map(...).orElseThrow()express? - 63.Why should integration tests clean up the data they create?
- 64.What does
Executors.newVirtualThreadPerTaskExecutor()change for I/O-heavy test drivers? - 65.Why does
Arrays.asList(intArray)with anint[]surprise people? - 66.Why should a test framework log the seed of random test data?
- 67.What is the cost of
synchronizedon a hot shared method in a parallel test runner? - 68.Why can a
finallyblock hide the original exception? - 69.When is
CopyOnWriteArrayLista good choice? - 70.What does reflection-based test tooling need in a modular (JPMS) app?
- 71.Why should you not rely on
finalize()for cleanup? - 72.What does
jcmd <pid> Thread.printor a JFR recording help diagnose in a slow suite? - 73.Why prefer
Instantand UTC in test assertions on timestamps? - 74.What does a 'golden master' (approval) test compare?
- 75.Why is a shared mutable
staticWebDriver an anti-pattern in a parallel Java test suite?