Java OCA OCP Practice Question 1627

Question

Which of the following cannot fill in the blank to make the code compile?

private void output(___ <?> x) {
   x.forEach(System.out::println);
}
  • A. ArrayDeque
  • B. Collection
  • C. TreeMap
  • D. None of the above


C.

Note

The forEach() method that takes one parameter is defined on the Collection interface.

A map is not a Collection.

There is a version of forEach() defined on the Map interface, but it uses two parameters.

Since two parameters can't be used with a method reference, Option C is the answer.




PreviousNext

Related