1Z0-819 Functional Interfaces 3 — Questions and Answers
Question 1: Which primitive-specialised functional interface avoids boxing when mapping an `int` to an `int`?
- IntFunction<Integer>
- Function<Integer,Integer>
- IntUnaryOperator (Correct answer)
- ToIntFunction<Integer>
Correct answer: IntUnaryOperator
IntUnaryOperator operates on primitive int values without boxing, whereas the other options involve wrapper types.
Question 2: What is the difference between `IntFunction<R>` and `ToIntFunction<T>`?
- IntFunction takes an int and returns R; ToIntFunction takes T and returns an int (Correct answer)
- IntFunction takes T and returns an int; ToIntFunction takes an int and returns R
- They are identical
- IntFunction only works with Integer objects
Correct answer: IntFunction takes an int and returns R; ToIntFunction takes T and returns an int
IntFunction<R> accepts a primitive int and produces a result R; ToIntFunction<T> accepts any T and produces a primitive int.
Question 3: Which interface does `UnaryOperator<T>` extend?
- Consumer<T>
- Function<T,T> (Correct answer)
- Supplier<T>
- Predicate<T>
Correct answer: Function<T,T>
UnaryOperator<T> extends Function<T,T>, specialising it for the case where input and output types are the same.
Question 4: What does `BinaryOperator.maxBy(Comparator<T> comparator)` return?
- A Comparator that finds the maximum
- A BinaryOperator that returns the greater of two elements according to the comparator (Correct answer)
- A Supplier that returns the max element
- A static int representing the max value
Correct answer: A BinaryOperator that returns the greater of two elements according to the comparator
BinaryOperator.maxBy returns a BinaryOperator that returns the larger of its two operands as determined by the given Comparator.
Question 5: Which of the following correctly declares a `DoubleSupplier` lambda?
- DoubleSupplier ds = () -> 3; (Correct answer)
- DoubleSupplier ds = (double x) -> x;
- DoubleSupplier ds = double::new;
- DoubleSupplier ds = Math::PI;
Correct answer: DoubleSupplier ds = () -> 3;
DoubleSupplier's abstract method getAsDouble() takes no arguments and returns a double; the integer literal 3 is widened automatically.
Question 6: What is the abstract method of `LongBinaryOperator`?
- apply(long t, long u)
- applyAsLong(long left, long right) (Correct answer)
- test(long t, long u)
- get(long t, long u)
Correct answer: applyAsLong(long left, long right)
LongBinaryOperator defines a single abstract method applyAsLong(long left, long right) returning a primitive long.
Question 7: Which functional interface should you prefer for `x -> x * 2` when `x` is a primitive `int`, to avoid autoboxing?
- Function<Integer,Integer>
- UnaryOperator<Integer>
- IntUnaryOperator (Correct answer)
- IntFunction<Integer>
Correct answer: IntUnaryOperator
IntUnaryOperator works entirely with primitive int values via applyAsInt(), eliminating all boxing overhead.
Which primitive-specialised functional interface avoids boxing when mapping an `int` to an `int`?