Lambdas & functional interfaces: interview questions & scenarios
These are the lambda questions I use to check whether you can connect the syntax to the runtime model: target typing, functional interfaces, method references, capture, and composition. Answer it out loud first, then open the model answer, and revisit the lessons when a detail feels fuzzy: Lambdas and functional interfaces, Method references, Effectively final and variable capture and Composing functions and chaining predicates.
What is a functional interface, and what does @FunctionalInterface actually do?
Tests SAM rule, Object methods, annotation purpose
Show answerHide answer
Model answer
A functional interface is an interface with exactly one abstract method. That single abstract method is the shape a lambda or method reference implements. Default methods and static methods do not count against the one-method rule, and public methods inherited from Object, like toString, do not create extra abstract methods for this purpose.
@FunctionalInterface is not required for lambdas to work. It is a compiler check and a signal to readers. If someone later adds a second abstract method, the annotated interface stops compiling instead of quietly becoming unusable as a lambda target.
@FunctionalInterface
interface PriceRule {
long apply(long cents);
default PriceRule andThen(PriceRule next) {
return cents -> next.apply(apply(cents));
}
}
How I’d say it in the interview
"A functional interface is a one-abstract-method interface. The annotation is optional, but I put it on my own callback interfaces because it locks that contract in at compile time. Defaults and statics are fine; a second abstract business method is not."
Signals depth
Mentioning that Java reused interfaces instead of adding a standalone function type.
Red flags
"It is any interface used by a lambda" or "the annotation creates the lambda behavior".
Follow-ups interviewers ask next
What if an interface has one abstract method and five default methods?
It is still functional. Default methods already have implementations, so they do not change the single abstract method count.
What if it declares
boolean equals(Object other)?That matches a public
Objectmethod and does not count as the functional method. In practice, avoid clever interfaces like that because they distract readers.
Why does s -> s.length() compile in one place and fail in another?
Tests target typing, poly expressions, primitive specialisations
Show answerHide answer
Model answer
A lambda gets its type from the target context: a variable declaration, method parameter, cast or return type. s -> s.length() can fit Function<String, Integer> because length() returns an int that can be boxed to Integer. It can also fit ToIntFunction<String> without boxing. It cannot fit Predicate<String> because a predicate must return boolean.
Function<String, Integer> boxedLength = s -> s.length();
ToIntFunction<String> primitiveLength = s -> s.length();
// Predicate<String> bad = s -> s.length(); // int is not boolean
The lambda text is not enough by itself. The target type pushes parameter and return expectations into the body.
How I’d say it in the interview
"The lambda is target typed. s -> s.length() is not born as a Function. If the target is ToIntFunction<String>, s is a String and the result is an int. If the target is Predicate<String>, the same body is wrong because it returns a number, not true or false."
Signals depth
Calling lambdas poly expressions and mentioning primitive functional interfaces to avoid boxing.
Red flags
"The compiler always knows s is String from the lambda" without naming the target.
Follow-ups interviewers ask next
What if I write
var f = s -> s.length();?That does not compile because
varneeds an initializer with a standalone type, and a lambda needs a target type. Use an explicit functional interface or a cast.What if two overloaded methods accept different functional interfaces?
The call can become ambiguous. Assign the lambda to a typed variable, cast it, or avoid API overloads that differ only by similar callback shapes.
What does this print, and why is the lambda this different from the anonymous class this?
Tests this binding, anonymous class, lambda scope
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Explain why a local variable used in a lambda must be final or effectively final.
Tests captured value, stack frame, reassignment rule
Model answer, how to say it, 2 follow-ups: unlock with the Java Pass
Translate these method references and name which ones are bound or unbound.
Tests method-reference forms, receiver position, constructor reference
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
When would you prefer a lambda over a method reference?
Tests readability, argument changes, constants, null handling
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What happens when you create a bound method reference from a null receiver?
Tests eager receiver evaluation, bound reference timing
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is the difference between Predicate.and, Predicate.or, negate and Predicate.not?
Tests predicate composition, short-circuiting, Java 11 helper
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Is filter(a).filter(b) the same as filter(a.and(b))?
Tests stream filters, composition, side effects
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is the difference between Function.andThen and Function.compose?
Tests composition order, naming, Function.identity
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Write a small rule combiner for signups where all configured predicates must pass.
Tests Predicate reduce, identity choice, domain default
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Why can AtomicInteger inside a lambda be a code smell?
Tests holder workaround, effectively final, reduction alternative
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Which standard functional interface would you choose for each callback?
Tests Function families, primitive variants, operator types
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal