Java OCA OCP Practice Question 1603

Question

What is the result of the following?

Comparator<Integer> c = (x, y) -> y-x;
List<Integer> ints = Arrays.asList(3, 1, 4);
Collections.sort(ints, c);
System.out.println(Collections.binarySearch(ints, 1));
  • A. 0
  • B. 1
  • C. The code does not compile.
  • D. The result is not defined.


D.

Note

A custom sort order is specified using a Comparator to sort in descending order.

However, this Comparator is not passed when searching.

When a different sort order is used for searching and sorting, the result is undefined.

Option D is correct.




PreviousNext

Related