Java OCA OCP Practice Question 1659

Question

What is the output of the following program?

package ai;//from  www .  j a va 2  s . c  om
import java.util.function.*;

public class Main {
   public void wakeUp(Supplier supplier) { // d1
      supplier.get();
   }
   public static void main(String... electricSheep) {
      Main data = new Main();
      data.wakeUp(() -> System.out.print("Program started!")); // d2
   }
}
  • A. Program started!
  • B. The code does not compile because of line d1 only.
  • C. The code does not compile because of line d2 only.
  • D. The code does not compile because of both lines d1 and d2.


C.

Note

The program does not compile, so Option A is incorrect.

The Supplier functional interface normally takes a generic argument, although generic types are not strictly required since they are removed by the compiler.

Line d1 compiles while triggering a compiler warning, and Options B and D are incorrect.

The line d2 does cause a compiler error, because the lambda expression does not return a value.

It is not compatible with Supplier, making Option C the correct answer.




PreviousNext

Related