What is the result?

Given the integer implements comparable:

What is the result?

Given the integer implements comparable:

What is the result?

A.
4
1

B.
1
2

C.
32

D.
21

E.
2
3

Explanation:

binarySearch
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
Searches the specified list for the specified object using the binary search algorithm.
The list must be sorted into ascending order according to the natural ordering of its elements (as
by the sort(List) method) prior to making this call. If it is not sorted, the results are undefined.
Parameters:

list – the list to be searched.
key – the key to be searched for.
Returns:
the index of the search key, if it is contained in the list; otherwise, (-(insertion point) – 1).



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Tim

Tim

D, but the option should be:

2
1

Since in program it uses println (not print).

Babak

Babak

please note that when you use binarySearch, you should use the same comparator as you sorted your list otherwise result is not guaranteed.

to get an accurate result, the last line should change to:

System.out.println(Collections.binarySearch(list, 3, integerComparator));