Java OCA OCP Practice Question 1677

Question

Which functional interface, when filled into the blank, allows the class to compile?

package sleep;/*w  ww . j  a v  a2 s .  co m*/
import java.util.function.*;
 
class Shape {}
public class Main {
   int MAX_SHEEP = 10;
   int c;
   public void count(____ f) {
      while(c<MAX_SHEEP) {
         // TODO: Apply lambda
         c++;
      }
   }
   public static void main(String[] dark) {
      new Main().count(System.out::println);
   }
}
  • A. Consumer<Shape>
  • B. Function<Shape,void>
  • C. UnaryOperator<Shape>
  • D. None of the above


A.

Note

The method reference System.

out::println takes a single input and does not return any data.

Consumer<Shape> is compatible with this behavior, making Option A the correct answer and Option D incorrect.

Option B is incorrect because void cannot be used as a generic argument.

Option C is incorrect since System.

out::println() does not return any data and UnaryOperator requires a return value.




PreviousNext

Related