aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes binary search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "binary search" Watch "binary search" New topic
Author

binary search

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
could any one help me out with the explanation of this question?

Given a properly prepared String array containing five elements, which range of results could a
proper invocation of Arrays.binarySearch() produce?
A. 0 through 4
B. 0 through 5
C. -1 through 4
D. -1 through 5
E. -5 through 4
F. -5 through 5
G. -6 through 4
H. -6 through 5

the answer is G.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
vijay umar wrote:could any one help me out with the explanation of this question?

Given a properly prepared String array containing five elements, which range of results could a
proper invocation of Arrays.binarySearch() produce?
A. 0 through 4
B. 0 through 5
C. -1 through 4
D. -1 through 5
E. -5 through 4
F. -5 through 5
G. -6 through 4
H. -6 through 5

the answer is G.

If the element is found: Return something from 0 to 4 (I hope this one is clear.)
If the element is not found, the index returned will be the index where the item should be inserted (substracting 1) so that the order of the array is not disrupted. It can be inserted anywhere from index 0 to index 4 (-1 to -5.) Additionally, it would be possible that this item is larger than any other element in the string, which means it would be inserted at index 5 (then, binarySearch() would return -6.)


All code in my posts, unless a source is explicitly mentioned, is my own.
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
Thanks a lot Ruben!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: binary search