Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Modern Java

    Interview Questions

    Real interview questions on Modern Java, answered the way you would say them out loud.

    Extra: Interview prep4 min read

    Interview questions

    What is a functional interface? How do lambdas relate to it?

    How I'd answer: "A functional interface has exactly one abstract method — default and static methods don't count. Comparator, Runnable, and the java.util.function ones like Predicate, Function, Consumer and Supplier are examples, and @FunctionalInterface makes the compiler enforce it. A lambda is an anonymous function, and it can be used wherever a functional interface is expected: the lambda becomes the implementation of that single method, with parameter types inferred from the interface. Lambdas can capture local variables only if they're effectively final, because the lambda gets a copy and may run later or on another thread. Under the hood they're compiled with invokedynamic, not as anonymous classes."

    Combines: Ch 1, 2.

    Are streams lazy? What does that mean in practice?

    How I'd answer: "Yes. Intermediate operations like filter and map just build up the pipeline; nothing runs until a terminal operation like toList, count or findFirst. Then elements flow through the pipeline one at a time rather than stage by stage, which enables short-circuiting: with filter, map and limit(2), only as many elements are processed as needed to produce two results — I've traced it, and later elements are never touched. Practical consequences: a stream with no terminal operation does nothing; you can work with infinite streams using limit; and sorted is a barrier that must consume everything first. Streams are also single-use."

    Combines: Ch 4, 7.

    What's the difference between map and flatMap?

    How I'd answer: "map applies a function that produces one output per element, so the stream keeps its shape — Stream<Student> to Stream<String> with Student::name. flatMap applies a function that produces a stream per element, and then flattens all those streams into one. For example, with a list of tag lists, map(List::stream) gives a stream of streams, while flatMap(List::stream) gives one stream of tags, which I can then distinct() and sort(). The same idea exists in Optional.flatMap, for functions that themselves return an Optional."

    Combines: Ch 5, 8.

    How should Optional be used? What's orElse vs orElseGet?

    How I'd answer: "Optional is meant as a return type for methods where no result is a normal outcome, like findByEmail, so callers are forced to handle the empty case with map, orElse, orElseGet or orElseThrow instead of forgetting a null check. I don't use it for fields, parameters or collections — for collections I return an empty collection — and I avoid calling get() without checking. orElse(x) always evaluates x, because it's just a method argument, even if a value is present; orElseGet(supplier) only calls the supplier when empty. So for an expensive default like a database lookup, orElseGet is the right choice."

    Combines: Ch 8.

    What are the most important features added between Java 8 and Java 21?

    How I'd answer: "Java 8 was the big functional shift: lambdas, method references, streams, Optional, default methods and java.time. Java 9 to 11 added collection factory methods like List.of, var, new String methods and the HTTP client. 14 to 16 brought switch expressions, text blocks, records and pattern matching for instanceof. Java 17 added sealed classes, and Java 21, the LTS most teams are moving to, completed the picture with pattern matching for switch and record patterns — which together with sealed types give exhaustive, compiler-checked switches — plus virtual threads, which make thread-per-request servers scale to huge numbers of concurrent blocking tasks, and sequenced collections with getFirst and reversed."

    Combines: Ch 9–11.