1Z0-819 Functional Interfaces 2 â Questions and Answers
Question 1: What is the correct return type of `BiFunction<String, Integer, Boolean>`?
- String
- Integer
- Boolean (Correct answer)
- void
Correct answer: Boolean
The third type parameter of BiFunction<T,U,R> is the return type R, so Boolean is returned.
Question 2: Given `Function<String,Integer> f = String::length;` and `Function<Integer,String> g = i -> "len:"+i;`, what does `f.andThen(g).apply("hi")` return?
- "len:hi"
- "len:2" (Correct answer)
- 2
- Compilation error
Correct answer: "len:2"
andThen applies f first (length=2) then g, producing "len:2".
Question 3: What is the difference between `Function.andThen(g)` and `Function.compose(g)`?
- andThen applies g before this function; compose applies g after
- andThen applies g after this function; compose applies g before (Correct answer)
- They are identical in behavior
- compose only works with UnaryOperator
Correct answer: andThen applies g after this function; compose applies g before
andThen(g) means this â g; compose(g) means g â this, reversing the application order.
Question 4: Which method on `Predicate<T>` returns a new Predicate that is the logical negation of the original?
- not()
- negate() (Correct answer)
- inverse()
- flip()
Correct answer: negate()
Predicate.negate() returns a new Predicate that represents the logical NOT of the original predicate.
Question 5: What does `Predicate.and(Predicate other)` short-circuit on?
- It short-circuits when the first predicate is true
- It short-circuits when the first predicate is false (Correct answer)
- It never short-circuits
- It short-circuits when the second predicate is true
Correct answer: It short-circuits when the first predicate is false
Predicate.and() short-circuits on false â if the first predicate returns false, the second is never evaluated.
Question 6: Which functional interface represents an operation that accepts two input arguments and returns no result?
- BiFunction<T,U,Void>
- BiConsumer<T,U> (Correct answer)
- BinaryOperator<T>
- BiSupplier<T,U>
Correct answer: BiConsumer<T,U>
BiConsumer<T,U> accepts two arguments and returns void, making it the two-argument form of Consumer.
Question 7: What happens when you call `consumer1.andThen(consumer2).accept(x)`?
- consumer2 is called first, then consumer1
- consumer1 is called first, then consumer2 (Correct answer)
- Both are called simultaneously
- Only consumer1 is called; consumer2 result is ignored
Correct answer: consumer1 is called first, then consumer2
Consumer.andThen() returns a composed Consumer that executes this consumer first, then the argument consumer.
What is the correct return type of `BiFunction`?