Java OCA OCP Practice Question 1681

Question

What is the output of the following application?

package lot;// w  w w  .  ja va  2 s .  c o m
import java.util.function.*;


public class Main {
   private int quantity = 40;
   private final BooleanSupplier stock;
   {
      stock = () -> quantity>0;
   }
   public void checkInventory() {
      if(stock.get())
         System.out.print("enough");
      else {
         System.out.print("not enough");
      }
   }
   public static void main(String... widget) {
      final Main w13 = new Main();
      w13.checkInventory();
   }
}
  • A. enough
  • B. not enough
  • C. The code does not compile because of the checkInventory() method.
  • D. The code does not compile for a different reason.


C.

Note

The primitive Supplier functional interfaces, such as BooleanSupplier and LongSupplier, do not have a get() method.

Instead, they have methods such as getAsBoolean() and getAsLong(), respectively.

For this reason, the first line of the checkInventory() method does not compile, making Option C the correct answer.

If the method call was changed to getAsBoolean(), then the rest of the code would compile without issue, print enough at runtime, and Option A would be the correct answer.




PreviousNext

Related