Lambdas and method references
23 questions in 7 subtopics.
Lambda syntax
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.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.When reviewing code, how do you decide whether a lambda is too long or complex? What do you suggest instead?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
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?What happens with this code and how would you rewrite it?int total = 0;
orders.forEach(o -> total += o.amount());
Method references
Explain the four kinds of method reference with an example of each: static, bound instance, unbound instance, and constructor.This code throwsNullPointerException 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.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
Name the core functional interfaces injava.util.function and their method signatures. Why do primitive specialisations such as IntPredicate exist?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?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?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
What are the differences between a lambda and an anonymous inner class? Consider this, scope, shadowing, serialization and performance.Your team converted hundreds of anonymous classes to lambdas with an IDE refactoring. One test now fails because a callback'stoString and identity behave differently. What changed and what should a reviewer look for in such refactorings?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
Can a lambda be serialized? What is required, and why is it discouraged?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
Make this compile and keep the IOException visible to the caller:List<String> contents = paths.stream().map(p -> Files.readString(p)).toList();
No question matches these choices.