This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
Similar Threads
k&b scjp5, page no 616, problem no 9
Chap 7 pg 628 from kathy & Sierra
K&B Book Chapter 7,Page 616,Question 9
Kathy Sierra doubt
K&B page 628 question 9