Dears,
I was practicing a mock test in Enthuware and i came across this question.
Select correct statements.
Options:
a.This is not a valid Comparator implementation.
b.Arrays.binarySearch(sa, "cc", new MyStringComparator()); will return -2.
c.Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0.
d.Arrays.binarySearch(sa, "c", new MyStringComparator()); will return -1.
e.Arrays.binarySearch(sa, "c", new MyStringComparator()); will throw an exception.
My answer was b and d. But the correct answer is b and c.
I am not able to understand why c is the correct answer. As per my understanding the binarysearch would search for the position where the correct element would exist if its not present and will calculate based on(-(index-1)
I am not sure if i am missing something. Can you please help me understand the same.
Thanks in Advance!!