Java OCA OCP Practice Question 1643

Question

What is the output of the following application?

package mypkg;/*from  ww  w.j  a v a 2  s .co m*/
import java.util.function.*;


class Shape {
   public Shape(double distance) {
      this.distance = distance;
   }
   public double distance;
}
public class Main {
   private void saveLife(Predicate<Shape> canSave, Shape tourist) {
      System.out.print(canSave.test(tourist) ? "Saved" : "Too far");  // y1
   }
   public final static void main(String... sand) {
      new Main().saveLife(s -> s.distance<4, new Shape(2));  // y2
   }
}
  • A. Saved
  • B. Too far
  • C. The code does not compile because of line y1.
  • D. The code does not compile because of line y2.


A.

Note

The code compiles without issue, so Options C and D are incorrect.

The value for distance is 2, which based on the lambda for the Predicate will result in a true expression, and Saved will be printed, making Option A correct.




PreviousNext

Related