Java OCA OCP Practice Question 1633

Question

How many lines does the following code output?

 import java.util.*;
class Shape {
   String color;//from   w  w  w  .ja  va 2 s  .c  om
   String getColor() {
      return color;
   }
}
public class Main {
   public static void main(String[] args) {
      Shape b1 = new Shape();
      Shape b2 = new Shape();
      b1.color = "pink";
      List<Shape> list = Arrays.asList(b1, b2);
      list.stream().filter(Shape::getColor).forEach(System.out::println);
   }
}
  • A. One
  • B. Two
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


C.

Note

The filter() method requires a boolean returned from the lambda or method reference.

The getColor() method returns a String and is not compatible.

This causes the code to not compile and Option C to be correct.




PreviousNext

Related