About Question com.enthuware.ets.scjp.v6.2.93 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.93 :

Post by ETS User »

Code: Select all

class MyStringComparator implements Comparator
{
    public int compare(Object o1, Object o2)
    {
	int s1 = ((String) o1).length();
	int s2 = ((String) o2).length();
	return s1 - s2;
    }
}

and 

static String[] sa = { "d", "bbb", "aaaa" };
The second option is one of the correct selections to this question, and it states:
Arrays.binarySearch(sa, "cc", new MyStringComparator()); will return -2.
Since there is no string of length 2 in sa, nothing in sa matches aa. So the return value has to be negative. Further, if aa were to be inserted in sa, it would have to be inserted after 'a' i.e. at index 1. Thus, the return value will be -(index+1) = -2.
....nothing in sa matches aa....Further, if aa were to be inserted....
I am confused. Where is "aa" mentioned in the code for this question, or are you referring to "cc" which is what is stated in this answer selection?

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

Re: About Question com.enthuware.ets.scjp.v6.2.93 :

Post by admin »

You are right. "aa" is actually "cc". The explanation has been fixed to the following:

Since there is no string of length 2 in sa, nothing in sa matches the string "cc". So the return value has to be negative. Further, if the values "cc" were to be inserted in sa, it would have to be inserted after "a" i.e. at index 1. Thus, the return value will be -(index+1) = -2.
If you like our products and services, please help us by posting your review here.

mark

Re: About Question com.enthuware.ets.scjp.v6.2.93 :

Post by mark »

I still see a problem with this revised answer. There is no 'a' in the problem. Since the sort order is 'd', 'bbb', and 'dddd', don't you mean 'd'? Shouldn't the answer read "Further, if the values "cc" were to be inserted in sa, it would have to be inserted after "d" ;) i.e. at index 1. Thus, the return value will be -(index+1) = -2."

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

Re: About Question com.enthuware.ets.scjp.v6.2.93 :

Post by admin »

Yes, it should be 'd'. Fixed. Thanks again for your feedback.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest