About Question enthuware.ocpjp.v7.2.1307 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocpjp.v7.2.1307 :
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.
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
The given answer is correct. The given comparator puts shorter strings before longer ones. You might want to try it out.
-Paul.
-Paul.
-
- Posts: 6
- Joined: Wed May 22, 2013 4:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Should this question be updated, because Comparator interface use generics?
Moreover it is not recommended to write code that ignores this.
Thanks,
your customer.
Moreover it is not recommended to write code that ignores this.
Thanks,
your customer.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
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.
HTH,
Paul.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
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.
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
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.
HTH,
Paul.
-
- Posts: 14
- Joined: Thu Apr 18, 2013 2:05 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
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.
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.
Last edited by admin on Tue Sep 10, 2013 2:32 pm, edited 1 time in total.
Reason: Deleted parts that add no value to the topic
Reason: Deleted parts that add no value to the topic
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Search assumes that the array is sorted. If it is not sorted, the output is unpredictable.arnoldnitesh wrote:Would you like to elaborate this without sorting array how can i predict output of binarySearch ?
The input is already sorted. There is no requirement that there has to be a call to sort before the search.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.
Last edited by admin on Tue Sep 10, 2013 2:33 pm, edited 1 time in total.
Reason: Deleted parts that add no value to the topic
Reason: Deleted parts that add no value to the topic
-
- Posts: 53
- Joined: Fri Sep 20, 2013 7:20 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
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

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

-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Hello,
An import statement has now been added to avoid this confusion. Thank you for your feedback!
Paul.
An import statement has now been added to avoid this confusion. Thank you for your feedback!
Paul.
-
- Posts: 53
- Joined: Fri Sep 20, 2013 7:20 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1307 :
Okey dokey, thanks 

Who is online
Users browsing this forum: No registered users and 4 guests