ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
String r = Collections.max(List.of("pear", "fig", "banana", "kiwi"),
                           Comparator.comparing(String::length));
System.out.print(r);
  1. A. banana
  2. B. pear
  3. C. fig
  4. D. kiwi

Correct answer: A. banana

Collections.max with a comparator returns the greatest element by that comparator, here the longest string. Without the comparator it would use alphabetical order and return pear.

More OOP with C++ and Java questions

Exams that ask this topic