Java OCA OCP Practice Question 1559

Question

What is the output of the following application?

package mypkg;//  w w w .  j  ava2  s. c o m
 
interface MyClass {
   int add(int x, int y);
   static int subtract(int x, int y) { return x-y; }
   default int multiply(int x, int y) { return x*y; }
}
public class Main {
   protected void calculate(MyClass add, int a, int b) {
      System.out.print(add.add(a, b));
   }
   public static void main(String[] moreNumbers) {
      final Main ti = new Main();
      ti.calculate((k,p) -> p+k+1, 2, 5);  // j1
   }
}
  • A. 8
  • B. The code does not compile because MyClass is not a functional interface.
  • C. The code does not compile because of line j1.
  • D. None of the above


A.

Note

The code compiles and runs without issue, printing 8 at runtime, making Option A correct and Option D incorrect.

The MyClass interface is a valid functional interface.

While it includes both static and default methods, it only includes one abstract method, the precise requirement for it to be considered a functional interface, making Option B incorrect.

Option C is incorrect because the lambda expression is valid and used correctly.




PreviousNext

Related