• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please Explain the Output

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is related to SCJP5.0.It is regarding binary search which includes insertionpoint concept.i am not clear with insertionpoint concept and if anyone know abt this concept,please share with me and explain the output of the following program.

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

Answer is G) -6 through 4.

i am not able to arrive at this output.please explain it in step by step .it will be helpful for me as i am preparing for SCJP5.0 certification.

Thanks in advance.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the concept of insertion point.
Consider the below example.
import java.util.*;


The output will be -6.
This is because if 8 were to be inserted it would have to be inserted after
"5" in the array.
Hence the insertion point(index of the number being searched) would be 5.
And the search result is given by -(insertion point)-1.
which is -(5)-1=-6.This is the maximum negative range.

The maximum positive range will be if you search for "5" which is at index 4.
Hence the possible range is -6 through 4.

Regards,
Joshua
 
sriannapoorna Balasubramanian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joshua.I am now clear with the concept of insertion point.Thanks for your effort.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From which SCJP 5.0 mock exam does this question come?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic