Java Core Middle interview questions and answers

75 questions for a Java Core (Middle) interview - with correct answers, explanations and links to the docs.

  1. 1.How does HashMap find a value by key?
  2. 2.You put an object into a HashSet, then change a field used in its hashCode. What happens?
  3. 3.What causes a ConcurrentModificationException here?
  4. 4.What is the 'load factor' of a HashMap?
  5. 5.What does PECS mean for generic wildcards?
  6. 6.What is type erasure?
  7. 7.Why make a defensive copy of a list passed to a constructor?
  8. 8.Is a record with a List field fully immutable?
  9. 9.What is the string pool?
  10. 10.What's a good practice when wrapping a low-level exception in your own?
  11. 11.Which use of Optional is considered a misuse?
  12. 12.What is the difference between orElse and orElseGet?
  13. 13.What does flatMap do in a stream?
  14. 14.What does this collector produce?
  15. 15.Why can parallel streams be a bad idea in a test framework or web server?
  16. 16.Why does a lambda need variables to be effectively final?
  17. 17.What is String::toUpperCase in map(String::toUpperCase)?
  18. 18.A class implements two interfaces with the same default method. What happens?
  19. 19.Why can a non-static inner class cause a memory leak?
  20. 20.What does protected allow?
  21. 21.What can an enum have besides constants?
  22. 22.What does pattern matching for instanceof save you?
  23. 23.What do sealed classes allow?
  24. 24.What does a switch with type patterns over a sealed interface give you?
  25. 25.What does volatile guarantee for a field?
  26. 26.What is a deadlock?
  27. 27.Why use an ExecutorService instead of creating threads by hand?
  28. 28.What does future.get() do on a Future?
  29. 29.What does CompletableFuture.thenApply do?
  30. 30.What is AtomicInteger for?
  31. 31.Why prefer ConcurrentHashMap over a HashMap shared by threads?
  32. 32.What is CountDownLatch useful for in tests?
  33. 33.Why is ThreadLocal popular for WebDriver in parallel Selenium tests?
  34. 34.Why must you call ThreadLocal.remove() in thread pools?
  35. 35.What does @ParameterizedTest with @ValueSource do in JUnit 5?
  36. 36.What does assertAll do in JUnit 5?
  37. 37.How do you check that code throws an exception in JUnit 5?
  38. 38.What is @Tag("smoke") used for in JUnit 5?
  39. 39.Why shouldn't tests depend on the order they run in?
  40. 40.What does dependsOnMethods do in TestNG?
  41. 41.What is a TestNG IRetryAnalyzer used for?
  42. 42.How do TestNG listeners help in UI test projects?
  43. 43.What does when(repo.find(1)).thenReturn(user) do in Mockito?
  44. 44.What does verify(mailer).send(any()) check?
  45. 45.What is an ArgumentCaptor for in Mockito?
  46. 46.What's the difference between a mock and a spy in Mockito?
  47. 47.What does <scope>test</scope> mean for a Maven dependency?
  48. 48.Which Maven plugin runs unit tests in the test phase?
  49. 49.What is the difference between the heap and the stack in the JVM?
  50. 50.What usually causes a StackOverflowError?
  51. 51.Why shouldn't you compare doubles with == in tests?
  52. 52.Which type should be used for money amounts?
  53. 53.Why does new BigDecimal("2.0").equals(new BigDecimal("2.00")) return false?
  54. 54.Why inject a Clock instead of calling LocalDate.now() directly?
  55. 55.What's the difference between Instant and LocalDateTime?
  56. 56.What does static initialization order mean for this code?
  57. 57.Why are mutable static fields risky in test code?
  58. 58.What is the danger of catching Exception in a test helper and returning null?
  59. 59.What does Comparator.comparing(User::getAge).thenComparing(User::getName) give?
  60. 60.What's wrong with stream().peek(...) for side effects?
  61. 61.What does reduce do in a stream?
  62. 62.Why is List.remove(1) on a List<Integer> confusing?
  63. 63.What does String.format("%.2f", 3.14159) return?
  64. 64.Why may a test comparing formatted numbers pass locally but fail in CI?
  65. 65.What is OutOfMemoryError: Java heap space a sign of?
  66. 66.Why are many tests failing with NoClassDefFoundError after a dependency upgrade?
  67. 67.What does Objects.equals(a, b) add over a.equals(b)?
  68. 68.What does @Nested in JUnit 5 allow?
  69. 69.How does JUnit 5 limit how long a test may run?
  70. 70.Why does a HashMap iteration order look random?
  71. 71.What does Collections.unmodifiableList(list) return?
  72. 72.What happens in this code at runtime?
  73. 73.What is the difference between Error and Exception?
  74. 74.Why can Thread.sleep(2000) in a test be a bad wait?
  75. 75.What does @BeforeAll need in JUnit 5 by default?