Java OCA OCP Practice Question 1665

Question

What is the output of the following application?

package tps;/*from   w  w  w  . j av  a2s  . c om*/
import java.util.*;


class Shape {
   private String name;
   public Shape(String name) {
      this.name = name;
   }
   public String getName() {return name.toUpperCase();}
   public String toString() {return getName();}
}
public class Main {
   public static void main(String[] reports) {
      final List<Shape> bosses = new ArrayList(8);
      bosses.add(new Shape("Jenny"));
      bosses.add(new Shape("Ted"));
      bosses.add(new Shape("Grace"));
      bosses.removeIf(s -> s.equalsIgnoreCase("ted"));
      System.out.print(bosses);
   }
}
A. [JENNY, GRACE]
B. [tps.Shape@4asd212c, tps.Shape@811239a]
C. The code does not compile because of the lambda expression.
D. The code does not compile for a different reason.


C.

Note

The lambda expression is invalid because the input argument is of type Shape, and Shape does not define an equalsIgnoreCase() method, making Option C the correct answer.

If the lambda was corrected to use s.

getName() instead of s, the code would compile and run without issue, printing [JENNY, GRACE] at runtime and making Option A the correct answer.




PreviousNext

Related