The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Binary Search (K&B Chap7 Self Test) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Binary Search (K&B Chap7 Self Test)" Watch "Binary Search (K&B Chap7 Self Test)" New topic
Author

Binary Search (K&B Chap7 Self Test)

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 153
Hello Ranchers,

Below is quesion #9 of K&B Chap 7 Self Test (p 616)


Given a properly prepared String array containing 5 elements, which range of results could be 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 <<-- correct answer
H. -6 through 5



I found a few threads tackling this same issue:
http://www.coderanch.com/t/269386/java-programmer-SCJP/certification/page
http://www.coderanch.com/t/263469/java-programmer-SCJP/certification/chapter

..but I am still lost... I couldn't even come up with a good answer that is E, as most of them answered.

Can anybody help me on this? Thank you!


SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCPJBCD in progress
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
What most people miss is the case where the insertion point would be
AFTER ALL existing elements in the collection--ie, index 5. You know
there are only five elements, so there is no index 5, only 0-4, but
that is the point.

That is, if you had an ArrayList with a,b,c,d and
e as its sorted elements, and then you searched on "f", the insertion
point would be index 5--that's where it would have gone if it had been
in the collection. Then you just negate it and subtract 1, due to the
zero thing, as explained in the book.


SCJP 88% | SCWCD 84%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary Search (K&B Chap7 Self Test)
 
Similar Threads
Question about binarySearch() at K&B5 book
Compiling errata for K&B, SCJP 6
Doubt in K&B SCJP 5: topic Converting Arrays to Lists to Arrays
Help me with SCJP 1.5
Doubt in K&B SCJP 5: topic File creation