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.How does
HashMapfind a value by key? - 2.You put an object into a
HashSet, then change a field used in itshashCode. What happens? - 3.What causes a
ConcurrentModificationExceptionhere? - 4.What is the 'load factor' of a
HashMap? - 5.What does PECS mean for generic wildcards?
- 6.What is type erasure?
- 7.Why make a defensive copy of a list passed to a constructor?
- 8.Is a record with a
Listfield fully immutable? - 9.What is the string pool?
- 10.What's a good practice when wrapping a low-level exception in your own?
- 11.Which use of
Optionalis considered a misuse? - 12.What is the difference between
orElseandorElseGet? - 13.What does
flatMapdo in a stream? - 14.What does this collector produce?
- 15.Why can parallel streams be a bad idea in a test framework or web server?
- 16.Why does a lambda need variables to be effectively final?
- 17.What is
String::toUpperCaseinmap(String::toUpperCase)? - 18.A class implements two interfaces with the same default method. What happens?
- 19.Why can a non-static inner class cause a memory leak?
- 20.What does
protectedallow? - 21.What can an enum have besides constants?
- 22.What does pattern matching for
instanceofsave you? - 23.What do sealed classes allow?
- 24.What does a switch with type patterns over a sealed interface give you?
- 25.What does
volatileguarantee for a field? - 26.What is a deadlock?
- 27.Why use an
ExecutorServiceinstead of creating threads by hand? - 28.What does
future.get()do on aFuture? - 29.What does
CompletableFuture.thenApplydo? - 30.What is
AtomicIntegerfor? - 31.Why prefer
ConcurrentHashMapover aHashMapshared by threads? - 32.What is
CountDownLatchuseful for in tests? - 33.Why is
ThreadLocalpopular for WebDriver in parallel Selenium tests? - 34.Why must you call
ThreadLocal.remove()in thread pools? - 35.What does
@ParameterizedTestwith@ValueSourcedo in JUnit 5? - 36.What does
assertAlldo in JUnit 5? - 37.How do you check that code throws an exception in JUnit 5?
- 38.What is
@Tag("smoke")used for in JUnit 5? - 39.Why shouldn't tests depend on the order they run in?
- 40.What does
dependsOnMethodsdo in TestNG? - 41.What is a TestNG
IRetryAnalyzerused for? - 42.How do TestNG listeners help in UI test projects?
- 43.What does
when(repo.find(1)).thenReturn(user)do in Mockito? - 44.What does
verify(mailer).send(any())check? - 45.What is an
ArgumentCaptorfor in Mockito? - 46.What's the difference between a mock and a spy in Mockito?
- 47.What does
<scope>test</scope>mean for a Maven dependency? - 48.Which Maven plugin runs unit tests in the
testphase? - 49.What is the difference between the heap and the stack in the JVM?
- 50.What usually causes a
StackOverflowError? - 51.Why shouldn't you compare doubles with
==in tests? - 52.Which type should be used for money amounts?
- 53.Why does
new BigDecimal("2.0").equals(new BigDecimal("2.00"))return false? - 54.Why inject a
Clockinstead of callingLocalDate.now()directly? - 55.What's the difference between
InstantandLocalDateTime? - 56.What does
staticinitialization order mean for this code? - 57.Why are mutable static fields risky in test code?
- 58.What is the danger of catching
Exceptionin a test helper and returning null? - 59.What does
Comparator.comparing(User::getAge).thenComparing(User::getName)give? - 60.What's wrong with
stream().peek(...)for side effects? - 61.What does
reducedo in a stream? - 62.Why is
List.remove(1)on aList<Integer>confusing? - 63.What does
String.format("%.2f", 3.14159)return? - 64.Why may a test comparing formatted numbers pass locally but fail in CI?
- 65.What is
OutOfMemoryError: Java heap spacea sign of? - 66.Why are many tests failing with
NoClassDefFoundErrorafter a dependency upgrade? - 67.What does
Objects.equals(a, b)add overa.equals(b)? - 68.What does
@Nestedin JUnit 5 allow? - 69.How does JUnit 5 limit how long a test may run?
- 70.Why does a
HashMapiteration order look random? - 71.What does
Collections.unmodifiableList(list)return? - 72.What happens in this code at runtime?
- 73.What is the difference between
ErrorandException? - 74.Why can
Thread.sleep(2000)in a test be a bad wait? - 75.What does
@BeforeAllneed in JUnit 5 by default?