1Z0-819 Lambda Expressions 5 — Questions and Answers
Question 1: Which of the following describes an 'effectively final' variable in the context of lambda capture?
- A variable declared with the final keyword only
- A variable whose value is never changed after its initial assignment (Correct answer)
- A variable that is static
- A variable annotated with @EffectivelyFinal
Correct answer: A variable whose value is never changed after its initial assignment
Effectively final means a variable that could have been declared final because it is never reassigned after initialization, even without the explicit final keyword.
Question 2: What is the difference between a lambda expression and an anonymous class instance in Java?
- Anonymous classes cannot implement functional interfaces; lambdas can.
- Lambdas do not introduce a new scope for 'this'; anonymous classes do. (Correct answer)
- Lambdas can extend abstract classes; anonymous classes cannot.
- There is no difference; they compile to the same bytecode.
Correct answer: Lambdas do not introduce a new scope for 'this'; anonymous classes do.
Inside a lambda, 'this' refers to the enclosing instance; inside an anonymous class, 'this' refers to the anonymous class instance itself.
Question 3: Which built-in functional interface accepts an int and returns an int without boxing?
- Function<Integer, Integer>
- IntUnaryOperator (Correct answer)
- IntSupplier
- ToIntFunction<Integer>
Correct answer: IntUnaryOperator
IntUnaryOperator's applyAsInt(int) method accepts and returns a primitive int, avoiding autoboxing overhead.
Question 4: Given: BiFunction<String, Integer, String> f = (s, n) -> s.repeat(n); what does f.apply("ab", 3) return?
- ababab (Correct answer)
- ab3
- aabbcc
- ab ab ab
Correct answer: ababab
String.repeat(n) repeats the string n times, so "ab".repeat(3) returns "ababab".
Question 5: What is printed by: Supplier<Double> s = Math::random; System.out.println(s.get() == s.get());
- true always
- false always
- true or false depending on random values (Correct answer)
- Compilation error
Correct answer: true or false depending on random values
Each call to s.get() invokes Math.random() independently, so the two values are extremely unlikely to be equal but could theoretically be.
Question 6: Which of the following correctly chains a lambda to transform a stream element and then filter it?
- stream.filter(x -> x > 0).map(x -> x * 2)
- stream.map(x -> x * 2).filter(x -> x > 0)
- Both A and B are valid but produce different results when filter precedes map (Correct answer)
- Neither; map and filter cannot be chained.
Correct answer: Both A and B are valid but produce different results when filter precedes map
Both chains are valid Java, but applying filter before or after map can yield different elements because the predicate tests different values in each ordering.
Question 7: In a lambda used with Optional.map(), what does the lambda receive and return?
- It receives an Optional and returns an Optional.
- It receives the unwrapped value and returns a new value; Optional.map wraps the result. (Correct answer)
- It receives the unwrapped value and must return an Optional.
- It receives nothing and returns the mapped value.
Correct answer: It receives the unwrapped value and returns a new value; Optional.map wraps the result.
Optional.map(Function<T,U>) passes the contained value to the function and wraps the returned value in a new Optional; the lambda itself does not deal with Optional directly.
Which of the following describes an 'effectively final' variable in the context of lambda capture?