Java OCA OCP Practice Question 2141

Question

Which one of the following functional interfaces can you assign the method reference Integer::parseInt?

The static method parseInt() in Integer class takes a String and returns an int, as in:

int parseInt(String s).
  • A. Bipredicate<string, Integer>
  • B. Function<Integer, string>
  • C. Function<string, Integer>
  • d. predicate<string>
  • e. Consumer<Integer, string>
  • F. Consumer<string, Integer>


C.

Note

the parseInt() method takes a String and returns a value, hence we need to use the Function interface because it matches the signature of the abstract method R apply(T t).

In Function<T, R>, the first type argument is the argument type and the second one is the return type.

Given that parseInt takes a String as the argument and returns an int (that can be wrapped in an Integer), we can assign it to Function<String, Integer>.




PreviousNext

Related