Scrivio Interview library

Lambdas and method references

23 questions in 7 subtopics.

Lambda syntax

ConceptFoundations7 min read
What is a lambda expression in Java, and what determines its type? Explain target typing with an example where the same lambda has two different types.
DebuggingFoundations7 min read
ExecutorService.submit is overloaded for Runnable and Callable. Which overload does each call use, and does each compile? (a) ex.submit(() -> doWork()) where doWork returns void; (b) ex.submit(() -> compute()) where compute returns int; (c) ex.submit(() -> { Files.delete(path); }) where Files.delete throws IOException.
DesignIntermediate7 min read
When reviewing code, how do you decide whether a lambda is too long or complex? What do you suggest instead?
LeadershipPrincipal6 min read
Lambdas came to Java in 2014, years after C# and Scala had similar features. Why did Java take so long, and what design choices from that time still affect how you write Java today?

Effectively final capture

ConceptFoundations7 min read
Why can lambdas only capture local variables that are final or effectively final? How do people work around it, and what bugs do the workarounds cause?
CodingFoundations6 min read
What happens with this code and how would you rewrite it?
int total = 0;
orders.forEach(o -> total += o.amount());
InternalsIntermediate7 min read
How does a lambda capture variables at the bytecode level? What is the difference between a non-capturing lambda, one that captures locals, and one that captures this?
LeadershipPrincipal7 min read
Java lambdas only capture effectively final locals, while some other languages allow mutable capture. Why did Java choose this, and what does it mean for coding standards around concurrency in functional code?

Method references

ConceptFoundations7 min read
Explain the four kinds of method reference with an example of each: static, bound instance, unbound instance, and constructor.
DebuggingIntermediate6 min read
This code throws NullPointerException at the line where the method reference is created, not when it is used: Supplier<String> s = config.getName()::trim; Explain why a lambda behaves differently.
LeadershipPrincipal6 min read
Teams argue about lambdas versus method references, long chains versus loops, and functional interfaces in APIs. As a principal engineer, how do you set a functional style guide that stays useful for many years without becoming a style war?

Java.util.function interfaces

ConceptFoundations7 min read
Name the core functional interfaces in java.util.function and their method signatures. Why do primitive specialisations such as IntPredicate exist?
DesignIntermediate6 min read
When should an API accept a standard interface such as Function<Order, Money> and when should it define its own functional interface such as PriceCalculator?
LeadershipAdvanced8 min read
Your shared libraries expose many methods taking Function, Supplier and Consumer. Consumers report that APIs are hard to understand and stack traces unclear. What guidelines would you set for functional parameters in public APIs?
LeadershipPrincipal6 min read
Java has no function types and instead uses many functional interfaces, including primitive specialisations. How does this affect library design and interoperability over years, and what would you standardise in your libraries?

Lambdas vs anonymous classes

ConceptFoundations7 min read
What are the differences between a lambda and an anonymous inner class? Consider this, scope, shadowing, serialization and performance.
ScenarioIntermediate6 min read
Your team converted hundreds of anonymous classes to lambdas with an IDE refactoring. One test now fails because a callback's toString and identity behave differently. What changed and what should a reviewer look for in such refactorings?
LeadershipAdvanced7 min read
Your organisation has a large Java 8-era codebase full of anonymous classes. Should you run an automated conversion to lambdas across all repositories? Weigh the benefits and risks and describe the rollout.

Lambda serialization

ConceptIntermediate6 min read
Can a lambda be serialized? What is required, and why is it discouraged?
DebuggingAdvanced7 min read
A distributed computing job serializes lambdas to send tasks to workers. After a small code change in an unrelated method, workers fail to deserialize tasks. Explain why and what design you would recommend for the platform.

Checked exceptions in lambdas

CodingFoundations6 min read
Make this compile and keep the IOException visible to the caller:
List<String> contents = paths.stream().map(p -> Files.readString(p)).toList();
DesignIntermediate7 min read
Different teams handle checked exceptions in lambdas with sneaky throws, wrapping helpers, and Either-like results. What would you standardise, and why?
DesignAdvanced6 min read
You are designing the public API of a shared data-processing library used by many teams. Callers' code throws checked exceptions such as IOException and SQLException. How do you design the functional parameters?