1Z0-819 Functional Interfaces 4 — Questions and Answers
Question 1: Which method reference form is used in `Arrays.sort(names, String::compareToIgnoreCase)`?
- Static method reference
- Instance method reference on a particular instance
- Instance method reference on an arbitrary instance of a type (Correct answer)
- Constructor reference
Correct answer: Instance method reference on an arbitrary instance of a type
String::compareToIgnoreCase is an instance method reference on an arbitrary instance — the first argument becomes the receiver of the method call.
Question 2: What functional interface does a constructor reference like `ArrayList::new` satisfy?
- Supplier<ArrayList> (Correct answer)
- Function<ArrayList, ArrayList>
- Consumer<ArrayList>
- UnaryOperator<ArrayList>
Correct answer: Supplier<ArrayList>
ArrayList::new with no arguments matches Supplier<ArrayList> because it takes no input and produces a new ArrayList instance.
Question 3: Given `Function<String, Integer> f = Integer::parseInt;`, what kind of method reference is this?
- Instance method reference on an arbitrary instance
- Instance method reference on a particular instance
- Static method reference (Correct answer)
- Constructor reference
Correct answer: Static method reference
Integer.parseInt is a static method, so Integer::parseInt is a static method reference.
Question 4: What does `Comparator.comparing(Person::getName)` return?
- A Predicate<Person> that tests name equality
- A Comparator<Person> that orders persons by their name (Correct answer)
- A Function<Person, String> extracting the name
- A Consumer<Person> that prints the name
Correct answer: A Comparator<Person> that orders persons by their name
Comparator.comparing accepts a key extractor Function and returns a Comparator that orders by the extracted key's natural order.
Question 5: Which code correctly creates a Comparator that sorts strings by length, then alphabetically?
- Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder()) (Correct answer)
- Comparator.comparing(String::length, String::compareTo)
- Comparator.naturalOrder().andThen(Comparator.comparing(String::length))
- Comparator.of(String::length).then(String::compareTo)
Correct answer: Comparator.comparing(String::length).thenComparing(Comparator.naturalOrder())
thenComparing chains a secondary Comparator used only when the primary comparison yields equal results.
Question 6: What does `Comparator.reversed()` do when chained with `Comparator.comparing(Person::getAge)`?
- Sorts by age in ascending order
- Sorts by age in descending order (Correct answer)
- Reverses the list in place
- Throws UnsupportedOperationException
Correct answer: Sorts by age in descending order
reversed() returns a Comparator that imposes the reverse ordering of the original, yielding descending age sort.
Question 7: An interface has one abstract method and one default method. Can it be annotated with `@FunctionalInterface`?
- No — any default method prevents it from being functional
- Yes — default methods do not count toward the single abstract method requirement (Correct answer)
- Only if the default method is declared private
- Only if the abstract method has no parameters
Correct answer: Yes — default methods do not count toward the single abstract method requirement
@FunctionalInterface requires exactly one abstract method; default and static methods do not affect this count.
Which method reference form is used in `Arrays.sort(names, String::compareToIgnoreCase)`?