- If the elements was found, you would get an index between 0 and 3 (because there are only 4 elements in the array) - If the element was not found, you would get a negative number. - According to the API, the negative is : (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list. - So the biggest index were the element can be inserted is 4 (at the tail). So the returned value is : (-(4) -1) = -5
I have tried but i am getting 0.
I don't understand that. Can you show us your command line ?
Originally posted by Prasad Tamirisa: Hi Narasingh, Welcome to javaranch. I agree with satou in this.
If we have got the answer -1 in the list,then that whould have been the aswer of this question.
As that is not the case, i alternate answer is 3.
If that is the logic of this question, then it is not testing our knowledge of the Java Collections.binarySearch method. If the question is about the possible results of the binarySearch method for a general input, then it is correct. As it stands we do not know the exact question as asked by the mock exam.