Java OCA OCP Practice Question 2178

Question

Choose the correct option based on this program:.

import java.util.function.BiFunction;

public class Main {
     public static void main(String args[]){
              BiFunction<String, String, Boolean> compareString = (x, y) ->
                x.equals(y);
             System.out.println(compareString.apply("Java8","Java8")); // #1
     }
}
  • A. this program results in a compiler error in line marked with #1
  • B. this program prints: true
  • C. this program prints: false
  • d. this program prints: (x, y) -> x.equals(y)
  • e. this program prints: ("Java8", "Java8") -> "Java8".equals("Java8")


B.

Note

the BiFunction interface takes two type arguments-they are of types String in this program.

the return type is Boolean.

BiFunction functional interface has abstract method named apply().

since the signature of String's equals() method matches that of the signature of the abstract method apply(), this program compiles fine.

When executed, the strings "Java8" and "Java8" are equal; hence, the evaluation returns true that is printed on the console.




PreviousNext

Related