Java OCA OCP Practice Question 3073

Question

Which one of the following statements will compile without any errors?

  • a) Supplier<LocalDate> now = LocalDate::now();
  • b) Supplier<LocalDate> now = () -> LocalDate::now;
  • c) String now = LocalDate::now::toString;
  • d) Supplier<LocalDate> now = LocalDate::now;


d)

Note

the now() method defined in LocalDate does not take any arguments and returns a LocalDate object.

hence, the signature of now() method matches that of the only abstract method in the Supplier interface: T get().

hence, the method reference Local::now can be assigned to Supplier<LocalDate> and the statement compiles without any errors.

other options show improper use of method reference and they will result in compiler error(s).




PreviousNext

Related