✨ Modern Java: 8 to 25 — interview questions

11 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.

11 of 11

01A profile endpoint reads `Optional<Profile> maybe = cache.find(id); return maybe.orElse(repository.loadAndCache(id));`. The cache hit rate is 97%, but the database shows a query on every single request, and it's writing to the cache on every one too. What's actually happening, and what's the fix?Optionaleasy0-2y02`Function<Integer, Integer> plus2 = x -> x + 2;` and `times3 = x -> x * 3;`. `plus2.andThen(times3).apply(5)` returns 21 and `plus2.compose(times3).apply(5)` returns 17 — same two functions, different answers. What's the rule, and which should application code default to?Functional compositioneasy0-2y03A reviewer approves `record Cart(String id, List<Item> items) {}` as immutable because every field is final. Months later, `cart.items().add(newItem)` works — no exception, and the cart really does have one more item. What actually happened, and what's the minimum fix inside the record's constructor?Immutabilitymedium3-5y04A service's `module-info.java` has `exports com.example.orders.api;` but Hibernate, reflecting into JPA entities in `com.example.orders.model`, throws `InaccessibleObjectException` at startup. A teammate proposes widening `exports` to cover the model package too. Is that the right fix, and what's actually different between `exports` and `opens`?Module systemmedium3-5y05A team upgrading 11 to 17 compiles with `-source 11 -target 11` on the new JDK, and the build passes clean. Weeks after deploying to a fleet still running Java 11, one code path throws `NoSuchMethodError` in production. What did `-source`/`-target` fail to catch, and what should they have used instead?JDK upgradesmedium3-5y06A batch job logs every record it processes with `records.stream().peek(r -> log.debug("processing {}", r.id())).count();`. On Java 8 the audit log was always complete. After a JDK upgrade, the count stays correct but the log goes silent — no exceptions, nothing in the logs. Why, exactly, and what's the fix?Streamsmedium5-8y07A custom `Collector.of(MinMax::new, MinMax::accept, (left, right) -> left)` finds min/max in one pass. It passes every test written with `.stream()`. Switched to `.parallelStream()` on a million elements, it returns the wrong maximum. What's the mechanism, and why did every sequential test pass?Collectorshard5-8y08A teammate says `x -> x + 2` "desugars to an anonymous inner class, same as Java 7's anonymous `Runnable`." Someone else notices the stack trace shows a class named `Composition$$Lambda/0x00000...`, not `Composition$1`. What does a lambda actually compile to, and why does the naming difference matter beyond cosmetics?Lambdas internalshard5-8y09A Spring Boot 3.2 service on Java 21 turns on virtual threads. An hour into peak traffic, with Redis running a slow failover, the service stops responding entirely — no errors logged, just silence. The rate limiter's `tryAcquire` method is `synchronized` and calls Redis inside the block. Explain the failure mechanism, and say what changes about it on Java 24.Virtual threadshard8+y10A team compiles `OrderService.java`, which uses `StructuredTaskScope` (a preview API in Java 21), with `javac --release 21 --enable-preview`. Their CI pipeline caches the resulting `.class` files across builds. After the JDK is bumped to 21.0.4 in the same major version, the cached class fails to load with an error that looks like build corruption. What's actually failing, and why doesn't matching the JDK's major version save them?Preview featureshard8+y11One thread repeatedly does `m.lo = i; m.hi = i;` on a shared mutable object; a reader thread checks whether it ever observes `lo != hi`. A measurement shows 4,090 torn reads with no locks anywhere. The same measurement with an immutable `Range` object — `shared = new Range(i, i);` swapped in by one reference write — shows zero. Why does the immutable version need no lock at all, and what could silently take that guarantee away?Concurrency and immutabilityhard8+y