Lambda Expressions Flashcards
7 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Lambda Expressions flashcards as text
Which functional interface should you use when a lambda takes two arguments of the same type and returns a result of the same type?
Answer: BinaryOperator
BinaryOperator extends BiFunction and is the correct specialization when both inputs and output share the same type.
What is the result of compiling and running: Predicate p = String::isEmpty; System.out.println(p.test(""));
Answer: true
String::isEmpty is a valid method reference to an instance method, and calling test("") invokes isEmpty() on the empty string, returning true.
Which statement about a lambda expression and its enclosing scope is TRUE?
Answer: Local variables captured by a lambda must be effectively final.
Lambdas can only capture local variables that are final or effectively final; instance and static variables are freely accessible.
Given: Function f = x -> x * 2; Function g = x -> x + 3; what does f.andThen(g).apply(5) return?
Answer: 13
andThen applies f first (5*2=10) then g (10+3=13).
Which of the following is NOT a valid lambda expression in Java 11?
Answer: (int x, y) -> x + y
When parameter types are explicitly listed you must provide types for all parameters; mixing typed and untyped parameters is a compile error.
What does Predicate.not(Predicate) introduced in Java 11 do?
Answer: Negates a predicate returning a new Predicate
Predicate.not(p) is a static factory that returns a new Predicate which is the logical negation of the given predicate.
Which functional interface represents a supplier that may throw a checked exception when used in a custom functional interface?
Answer: Callable
Callable declares throws Exception on its call() method, while Supplier's get() does not declare a checked exception.