About Question enthuware.ocpjp.v8.2.1307 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
nelsonct
Posts: 2
Joined: Sun Sep 13, 2015 9:21 pm
Contact:

About Question enthuware.ocpjp.v8.2.1307 :

Post by nelsonct »

We have the array

Code: Select all

 static String[] sa = { "d", "bbb", "aaaa" }; 
Why is the answer "Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0." correct? To my understanding, "c" does not exist and will be inserted at index 0, thus Arrays.binarySearch(sa, "c", new MyStringComparator()); will return -(0)-1 = -1. However, that answer was marked wrong.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by admin »

Why do you think "c" will be inserted at index 0? The explanation tells exactly where it will be inserted: "The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key,..."
If you like our products and services, please help us by posting your review here.

Elecded
Posts: 2
Joined: Wed May 10, 2017 9:11 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by Elecded »

binarySearch would return 0 if "c" would be found in array at index 0, right?

The first element greater than "c" is "bbb" with index 1. Then binarySearch should return -(1+1) = -2.

<EDIT> The explanation says exactly that about return value 0: "Note that this guarantees that the return value will be >= 0 if and only if the key is found. >

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by thodoris.bais »

That's the thing, as Elecded explained.

It might be still somehow confusing in my mind, but I believe the reason behind 0 as the search index for "c" key is that our comparator is based on length and not on the natural (alphabetical) order of elements. "d" is of the same length and the key that we search, so, our custom Comparator class returns 0

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by admin »

thodoris.bais wrote:That's the thing, as Elecded explained.

It might be still somehow confusing in my mind, but I believe the reason behind 0 as the search index for "c" key is that our comparator is based on length and not on the natural (alphabetical) order of elements. "d" is of the same length and the key that we search, so, our custom Comparator class returns 0
Your understanding is correct.
If you like our products and services, please help us by posting your review here.

Bikfic
Posts: 2
Joined: Mon Apr 16, 2018 12:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by Bikfic »

To my understanding

Code: Select all

Arrays.binarySearch
only works if the array has been previously sorted. ("The array must be sorted (as by the

Code: Select all

sort(byte[])
method) prior to making this call." --> Otherwise)
The array

Code: Select all

String[] strings = {"d", "bbb", "aaaa"};
is not sorted. Why is the result not "undefined" then?

Thank you!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by admin »

The array needs to be sorted as per the comparator you are using to search. In this case, we are using MyComparator, which compares the length of the string and according to this comparator, the array is indeed sorted.
If you like our products and services, please help us by posting your review here.

Bikfic
Posts: 2
Joined: Mon Apr 16, 2018 12:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by Bikfic »

Thanks! :cheers: :cheers:

dongyingname
Posts: 18
Joined: Sat Jun 22, 2019 4:10 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Post by dongyingname »

Arrays.binarySearch() method returns the index of the search key, if it is contained in the list.
Would explain why

Code: Select all

Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0
is correct.

The "key" in
returns the index of the search key,
is defined by the comparator. In our case, the "key" is actually meant to be .length(), which is equal to 1 as to "d".
If "d" is passed into the method, 0 is returned because the string of length that equals to 1 is contained in the list and is located at index 0. From the Comparator point of view, the array is actually like {1, 3, 4} just because or compare() method forces the it to compare elements of the { "d", "bbb", "aaaa" } array by length.
Trying passing "d" (length=1), "ddd" (length=3)and "dddd" (length=4)into the call would return 0, 1, 2 respectively, which agrees to the quote and my conclusion.

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests