Java OCA OCP Practice Question 2109

Question

determine the behavior of this program:.

public class Main {
    @FunctionalInterface/*from  w  w w. ja  v a  2 s. c o  m*/
    interface LambdaFunction {
        int apply(int j);
        boolean equals(java.lang.Object arg0);
    }

    public static void main(String []args) {
        LambdaFunction lambdaFunction = i -> i * i;    // #1
        System.out.println(lambdaFunction.apply(10));
    }
}
  • a . this program results in a compiler error: interfaces cannot be defined inside classes
  • B. this program results in a compiler error: @FunctionalInterface used for lambdaFunction that defines two abstract methods
  • C. this program results in a compiler error in code marked with #1: syntax error
  • d. this program compiles without errors, and when run, it prints 100 in console


D.

Note

an interface can be defined inside a class.

the signature of the equals method matches that of the equal method in Object class; hence it is not counted as an abstract method in the functional interface.

it is acceptable to omit the parameter type when there is only one parameter and the parameter and return type are inferred from the lambdaFunction abstract method declaration int apply(int j).

since the lambda function is passed with the value 10, the returned value is 10 * 10, and hence 100 is printed in console.




PreviousNext

Related