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