Page 1 of 1
About Question enthuware.ocpjp.v7.2.1307 :
Posted: Fri Mar 08, 2013 6:02 am
by fabiovh
I believe the answer is incorrect.
The comparator places longer strings before shorter ones. So the given array is not sorted, according to the comparator. There is no sorting performed. So the result is undefined (according to JavaDocs) when binarySearch is called in an unsorted array.
If the comparator was return s2-s1 then the answer would have been correct.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Fri Mar 08, 2013 6:38 am
by admin
The given answer is correct. The given comparator puts shorter strings before longer ones. You might want to try it out.
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Fri Mar 08, 2013 8:51 am
by fabiovh
You're right. My bad.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed May 22, 2013 4:06 am
by tduglas
Should this question be updated, because Comparator interface use generics?
Moreover it is not recommended to write code that ignores this.
Thanks,
your customer.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Wed May 22, 2013 7:39 pm
by admin
Yes, it is better to use generics where ever possible but it is not a requirement for every code snippet. You may get a code snippet in the exam that doesn't use generics.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Fri Jul 26, 2013 2:44 pm
by The_Nick
When searching for "c" and it sees a "d" it does hence "c".length()-"d".length() = 0 It should be inserted at 0 and then -1 to avoid 0 values. I understand that.
What if instead of searching I was inserting "c". Where would "c" be placed before "d" or after? the comparator says they are equal... it's going to look for natural order? and if comparable it's not implemented and comparator is the only measure available?
Thanks in advance.
The_Nick.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Fri Jul 26, 2013 3:02 pm
by admin
The insertion place would be determined by the implementation of the collection class that allows insertion. There is no single "insert" method that works the same way for all the collections.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sat Sep 07, 2013 12:28 pm
by arnoldnitesh
Would you like to elaborate this without sorting array how can i predict output of binarySearch ?
first thing you are not sorting array at all , even if it was sorted that was not enough, you have to pass comparator for sorting also.while you did nothing here as should be done.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sat Sep 07, 2013 1:29 pm
by admin
arnoldnitesh wrote:Would you like to elaborate this without sorting array how can i predict output of binarySearch ?
Search assumes that the array is sorted. If it is not sorted, the output is unpredictable.
arnoldnitesh wrote:
first thing you are not sorting array at all , even if it was sorted that was not enough, you have to pass comparator for sorting also.while you did nothing here as should be done.
The input is already sorted. There is no requirement that there has to be a call to sort before the search.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sun Sep 22, 2013 7:18 am
by Student
This code won't compile. Where's the import statement? You can't just code "implements Comparator". Either you import java.util.Comparator/java.util.* or you implement java.util.Comparator.
I don't think it's sufficient to say "you need to assume necessary import statements" because that isn't stated in the questions and there are questions that test on missing import statements, or knowledge of what package a particular class in.
E.g. you could get a question that tests on the fact that Comparable needs no import statement but Comparator does.
/grumble

Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sun Sep 22, 2013 9:30 am
by admin
Hello,
An import statement has now been added to avoid this confusion. Thank you for your feedback!
Paul.
Re: About Question enthuware.ocpjp.v7.2.1307 :
Posted: Sun Sep 22, 2013 9:57 am
by Student
Okey dokey, thanks
