About Question enthuware.ocpjp.v8.2.1477 :

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

Moderator: admin

Post Reply
johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

About Question enthuware.ocpjp.v8.2.1477 :

Post by johnlong »

Hi
Explanation says : On the other hand, if a a separate object of type Comparator is used, the collection of X objects can be sorted in any way depending on what Comparator you use.
How can we be sure that class does not use separate Comparator for sorting?


Since compareTo method is used for sorting that means, Class \\\X\\\ implements \\\Comparable\\\ interface.
Class may not necessary implement Comparable if it has compareTo method, and class may use Comparator as well in this situation.
In if this true - correct answer would be
Class X neither implements Comparable nor Comparator. Object of a separate class that implements Comparator is used for sorting.

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

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

Post by admin »

The problem statement clearly tells you that the compareTo() method is used while sorting the given collection.
If a Comparator is used to sort, then it would have used compare() method and not compareTo.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong »

I see, thanks.
I guess I missed the problem statement.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

The mechanism used in this situation allows the Objects of class X to be sorted in only one way.
Not true, not at all. You can use the fact that a class implements compareTo by invoking Comparator.reverseOrder() to retrieve a comparator that can be used in Collections.sort(list,comparator) to sort the list in reverse order.

https://docs.oracle.com/javase/8/docs/a ... rseOrder--

There are questions where we are expected to conceive of things, circumstances, possibilities that are not in the question, and if we do not do so we get it wrong. I do not see why this question is different. It is inconsistent.

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

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

Post by admin »

Almost everything can be done using other code and classes. The question is about the given situation.

Having said that, the question clearly says that when the list is sorted, compareTo method is used. So, the Comparator interface is not being used here.
Can you show code that will sort the list in any other way using the same compareTo method?
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post by __JJ__ »

Code: Select all

    static void f2(){
        class X implements Comparable<X> {
            int i; 
            X(int i) { this.i=i;}
            public String toString() { return "" + i;}
            public int compareTo(X that) { 
                System.out.printf("in compareTo, this.i=%d, that.i=%d\n",this.i,that.i); 
                return this.i == that.i? 0 : this.i < that.i? -1 : 1; 
            }
        }
        List<X> list = Arrays.asList(new X[]{new X(3),new X(1), new X(9)}); //needs the new[]
        list.sort(Comparator.reverseOrder());
        System.out.println(list);
    }
output:

Code: Select all

in compareTo, this.i=3, that.i=1
in compareTo, this.i=1, that.i=9
in compareTo, this.i=1, that.i=9
in compareTo, this.i=3, that.i=9
[9, 3, 1]
"when the list is sorted, compareTo method is used"

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

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

Post by admin »

I truly applaud your effort in proving your point.

The sort method that you have used in your code is defined in List interface (newly added in 1.8), while the question says you have a collection. Even so, since a List is a collection, even one case as shown by your code makes this option incorrect.

I will ask the author to update this question asap.

many thanks!
Paul.
If you like our products and services, please help us by posting your review here.

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post by jbilkes »

"Class X neither implements Comparable not Comparator" should be "Class X neither implements Comparable nor Comparator" i think

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

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

Post by admin »

Yes, it should be nor instead of not but I see that it has already been fixed.
thanks!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 38 guests