Page 1 of 1

About Question enthuware.ocpjp.v7.2.1306 :

Posted: Mon Jan 09, 2017 8:58 am
by jagoneye
Can you please explain to me what is the purpose of this method??

Code: Select all

binarySearch(T[] a, T key, Comparator<? super T> c)
Why do you need to provide a Comparator when the requirement to search is that
you need the array or the list to be sorted first and you can use the Comparator
during sorting. So i don't understand the practical use of this method.
Another question is that in case of this question, if I pass anything other than String in binarysearch say

Code: Select all

Arrays.binarySearch(sa, 5)
it compiles without any errors whereas if the same array was of primitive type it
doesn't compile if I pass other than the primitive type. I'm guessing it has something to do with strongly typed primitive arrays while String not being a data type.

Re: About Question enthuware.ocpjp.v7.2.1306 :

Posted: Mon Jan 09, 2017 11:47 am
by admin
1. How will you compare the object that you are searching for with the objects in the array?
2. The call in being bound to another binarySearch method binarySearch(Object[] a, Object key)

Re: About Question enthuware.ocpjp.v7.2.1306 :

Posted: Fri Apr 02, 2021 2:59 am
by waraging
why is the question giving a very ambiguous with the word (independent of each other or together) that make people read that it should be separate answer

Re: About Question enthuware.ocpjp.v7.2.1306 :

Posted: Fri Apr 02, 2021 3:29 am
by admin
It just means that you could either use each option independently or use them together to achieve the desired output.

Re: About Question enthuware.ocpjp.v7.2.1306 :

Posted: Mon Jan 30, 2023 4:31 am
by asi-aal
waraging wrote:
Fri Apr 02, 2021 2:59 am
why is the question giving a very ambiguous with the word (independent of each other or together) that make people read that it should be separate answer
I agree, for me it sounded the same

Re: About Question enthuware.ocpjp.v7.2.1306 :

Posted: Thu Apr 11, 2024 10:46 pm
by pavvel
I disagree with the words in the question: (independent of each other or together).
We can use only binarySearch in this option. And binarySearch should work with sorted collection to return 2.
So, there is no independent case. Only together will it work.
Am I correct?

Re: About Question enthuware.ocpjp.v7.2.1306 :

Posted: Thu Apr 11, 2024 10:55 pm
by admin
The code will print 2 only if you insert both these options together (not independently):
Arrays.sort(sa);
System.out.println(Arrays.binarySearch(sa, "charlie"));

You have to select 2 options.

One could also select System.out.println(Arrays.linearSearch(sa, "andy")); and System.out.println(Arrays.binarySearch(sa, "charlie")) thinking that both will work if they are inserted independently but that would be wrong because neither of them will achieve the goal.

So, the correct answer is to select 3 and 4.