Java OCA OCP Practice Question 1719

Question

Which of the following can be the type for x?

private static void spot(____ x) {
   x.filter(y -> ! y.isEmpty())
     .map(y -> 8)
     .ifPresent(System.out::println);
}
  • I. List<String>
  • II. Optional<Collection>
  • III. Optional<String>
  • IV. Stream<Collection>
  • A. I
  • B. IV
  • C. II and III
  • D. II and IV


C.

Note

List doesn't have a filter() method, so Option A is incorrect.

Stream does have filter() and map() methods.

Stream doesn't have an ifPresent() method.

This makes IV incorrect, so Options B and D are incorrect.

Both Collection and String have an isEmpty() method, so either can be used with the Optional, making Option C the answer.




PreviousNext

Related