1Z0-819 Lambda Expressions 4 — Questions and Answers
Question 1: Which of the following lambda expressions is equivalent to the method reference Integer::parseInt?
- s -> Integer.parseInt(s) (Correct answer)
- () -> Integer.parseInt()
- s -> s.parseInt()
- (Integer i) -> parseInt(i)
Correct answer: s -> Integer.parseInt(s)
Integer::parseInt is a static method reference equivalent to the lambda s -> Integer.parseInt(s).
Question 2: A lambda that takes no arguments and returns void corresponds to which functional interface?
- Supplier<Void>
- Function<Void,Void>
- Runnable (Correct answer)
- Consumer<Void>
Correct answer: Runnable
Runnable's run() method takes no parameters and returns void, making it the natural fit for () -> { ... } style lambdas.
Question 3: What does Function.identity() return?
- A function that always returns null
- A function that returns its input argument unchanged (Correct answer)
- A function that returns 0 for numeric types
- A predicate that always returns true
Correct answer: A function that returns its input argument unchanged
Function.identity() returns a Function that always returns its input argument (t -> t).
Question 4: Given: Predicate<Integer> isEven = n -> n % 2 == 0; Predicate<Integer> isPositive = n -> n > 0; which call correctly creates a predicate for even AND positive numbers?
- isEven.or(isPositive)
- isEven.and(isPositive) (Correct answer)
- isEven.negate().and(isPositive)
- Predicate.not(isEven).and(isPositive)
Correct answer: isEven.and(isPositive)
Predicate.and() returns a composed predicate that represents a short-circuit logical AND of both predicates.
Question 5: Which of the following is TRUE about serializing lambda expressions in Java?
- All lambdas are serializable by default.
- Lambdas are serializable only if the target functional interface extends Serializable. (Correct answer)
- Lambda serialization is not supported in Java 11.
- Lambdas require @FunctionalInterface to be serializable.
Correct answer: Lambdas are serializable only if the target functional interface extends Serializable.
A lambda is serializable if and only if its target type (the functional interface) extends java.io.Serializable.
Question 6: What is the output of: List.of("a","bb","ccc").stream().filter(s -> s.length() > 1).count();
- 1
- 2 (Correct answer)
- 3
- 0
Correct answer: 2
filter keeps "bb" and "ccc" (lengths 2 and 3, both > 1), so count() returns 2.
Question 7: When using a lambda to implement an interface with a throws clause on the abstract method, what must the lambda body do?
- Always throw the declared exception
- Handle or propagate only checked exceptions declared by the functional interface method (Correct answer)
- Wrap all exceptions in RuntimeException
- Declare its own throws clause with @throws
Correct answer: Handle or propagate only checked exceptions declared by the functional interface method
The lambda body can throw checked exceptions only if the functional interface's abstract method declares them; otherwise only unchecked exceptions are permitted.
Which of the following lambda expressions is equivalent to the method reference Integer::parseInt?