1Z0-819 Lambda Expressions 3 — Questions and Answers
Question 1: What is the target type of a lambda expression?
- The type of the first parameter
- The functional interface type inferred from context (Correct answer)
- Always java.util.function.Function
- The return type of the lambda body
Correct answer: The functional interface type inferred from context
The target type is the functional interface that the compiler infers from the assignment, method argument, or cast context where the lambda appears.
Question 2: Given: Consumer<String> c1 = s -> System.out.print(s); Consumer<String> c2 = s -> System.out.print(s.length()); Consumer<String> combined = c1.andThen(c2); combined.accept("Hi"); — what is printed?
- Hi2 (Correct answer)
- 2Hi
- Hi
- 2
Correct answer: Hi2
Consumer.andThen chains consumers in order, so c1 prints "Hi" then c2 prints "2", producing "Hi2".
Question 3: Which of the following correctly uses a constructor reference?
- Supplier<List<String>> s = ArrayList::new; (Correct answer)
- Supplier<List<String>> s = new ArrayList::();
- Function<List<String>> s = ArrayList::new;
- Supplier<List<String>> s = () -> ArrayList;
Correct answer: Supplier<List<String>> s = ArrayList::new;
ArrayList::new is a valid constructor reference that matches the Supplier<List<String>> functional interface because ArrayList has a no-arg constructor.
Question 4: In Java 11, which method of Function<T,R> allows composing a function BEFORE the current one?
- andThen
- compose (Correct answer)
- apply
- identity
Correct answer: compose
Function.compose(before) returns a composed function that applies 'before' first and then applies the current function to the result.
Question 5: What happens if you assign the same lambda body to two different compatible functional interface variables?
- Compilation error because lambdas have a fixed type
- It works; the lambda adopts each interface's target type independently (Correct answer)
- RuntimeException is thrown
- Both variables point to the same lambda instance
Correct answer: It works; the lambda adopts each interface's target type independently
A lambda expression is poly-expression and can be assigned to any compatible functional interface; each assignment creates an independent instance.
Question 6: Which statement about Comparator.comparing() used with a lambda is TRUE?
- It accepts a Supplier to extract the sort key.
- It returns a Comparator built from a key-extraction Function. (Correct answer)
- It only works with primitive types.
- It is deprecated in Java 11.
Correct answer: It returns a Comparator built from a key-extraction Function.
Comparator.comparing(Function<T,U> keyExtractor) creates a Comparator that compares by the extracted key, commonly used with lambdas or method references.
Question 7: What is the output of: IntStream.of(1,2,3).map(x -> x * x).sum();
- 6
- 14 (Correct answer)
- 9
- 36
Correct answer: 14
The lambda squares each element (1, 4, 9) and sum() adds them: 1+4+9=14.
What is the target type of a lambda expression?