Page 1 of 1

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

Posted: Thu Apr 18, 2013 2:10 pm
by arnoldnitesh
your explanation regarding option 5 is ridiculous.Your question clears that you are asking in context of com.enthu package class not outside of that.

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

Posted: Thu Apr 18, 2013 7:27 pm
by admin
The given explanation is correct and relevant. The equals method of a class can be called from another other class in any other package and not just from the same class.

I think the question covers a fine point about the equals method very well. A novice programmer can easily fall into the trap illustrated by this question.

-Paul.

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

Posted: Wed Sep 25, 2013 5:17 am
by Student
"Two distinct Resource objects may be considered "not equal" even if their data values are same."

This is an interesting question and I get what it's trying to test, but the wording is ambiguous. What is implicit in the above question is the subject of the verb "considered". I.e., may be considered "by what" or "by whom"? Certainly any code that uses Resource.equals will consider two objects with the same data values as equal. What really is being tested is the knowledge that collections will use boolean equals(Object o) but unfortunately this knowledge is tested somewhat indirectly via the absence of the subject in the question. Well at least the question has done its job; I won't fall for this trap again :)

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

Posted: Wed Sep 25, 2013 7:44 am
by admin
Student wrote: Certainly any code that uses Resource.equals will consider two objects with the same data values as equal.
Again, as mentioned in the explanation code that "calls" Resource.equals may not necessary invoke the equals method that you think would be invoked.

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

Posted: Thu Jan 29, 2015 4:51 pm
by romsky
Best question so far!

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

Posted: Tue Feb 24, 2015 10:35 am
by jmrego
Nice question and even best explanation. Very very tricky.

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

Posted: Wed Aug 05, 2015 3:17 am
by colmkav
"Conversely, if two distinct Resource objects are considered equal, it means that the comparison has been done from a class that belongs to com.enthu package and their data fields contain the same value. Therefore, option 1 is correct."

Surely, if 2 distinct Resource objects are to be considered equal, it means we use the public boolean equals(Object obj) method? I think that still means the data fields will be equal (ie refers to the same object) so same answer but don't agree with the above rationale.

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

Posted: Wed Aug 05, 2015 6:50 am
by admin
colmkav wrote: Surely, if 2 distinct Resource objects are to be considered equal, it means we use the public boolean equals(Object obj) method? I think that still means the data fields will be equal (ie refers to the same object) so same answer but don't agree with the above rationale.
Sorry, I am unable to understand what you mean.

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

Posted: Wed Aug 05, 2015 8:30 am
by colmkav
admin wrote:
colmkav wrote: Surely, if 2 distinct Resource objects are to be considered equal, it means we use the public boolean equals(Object obj) method? I think that still means the data fields will be equal (ie refers to the same object) so same answer but don't agree with the above rationale.
Sorry, I am unable to understand what you mean.
Why is the explanation talking about the "boolean equal(Resource)" method when discussing whether 2 Resource objects are considered equal? Surely, it is the method "public boolean equal(Object)" that should be used.

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

Posted: Wed Aug 05, 2015 10:23 am
by admin
Explanation is talking about equals(Resource ) because that is how the method is coded in the given code (the given class does not override equals(Object ) method). This method is causing the problem discussed in the explanation and above.
I am still not sure what you mean by "should be used". Do you mean the JVM should automatically figure out which method should be used or do you mean the developer should code only equals(Object ) method and should not code equals(Resource )?

Anyway, the fact is that the given code contains equals(Resource ) method and the question expects you to determine what will happen. The explanation explains exactly what happens. Please go through it.

-Paul.

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

Posted: Wed Aug 05, 2015 10:44 am
by colmkav
admin wrote:Explanation is talking about equals(Resource ) because that is how the method is coded in the given code. This method is causing the problem discussed in the explanation and above.
I am still not sure what you mean by "should be used". Do you mean the JVM should automatically figure out which method should be used or do you mean the developer should code only equals(Object ) method and should not code equals(Resource )?

Anyway, the fact is that the given code contains equals(Resource ) method and the question expects you to determine what will happen. The explanation explains exactly what happens. Please go through it.

-Paul.
I understand the explanation. I just disagree with the assumption made in it. Why would we assume to be in the com.enthu package? If we do the comparison outside the package then we clearly use the "usual" equals method using Object as a parameter. The answer is still the same so its an academic point.

"if two distinct Resource objects are considered equal, it means that the comparison has been done from a class that belongs to com.enthu package"

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

Posted: Wed Aug 05, 2015 11:11 am
by admin
No, it is not an academic point. If you do the comparison from outside com.enthu package, two distinct Resource objects will NOT be considered equal. That is the whole point of the question!
You have not understood the explanation. I would suggest you to go through the whole explanation once more.

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

Posted: Wed Aug 05, 2015 11:47 am
by colmkav
admin wrote:No, it is not an academic point. If you do the comparison from outside com.enthu package, two distinct Resource objects will NOT be considered equal. That is the whole point of the question!
You have not understood the explanation. I would suggest you to go through the whole explanation once more.
Ah ok, it does say 2 "distinct" objects. I understand. thx

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

Posted: Mon Aug 24, 2015 1:56 am
by Ad9999
Very good question. Didn't consider what would happen if you call equals from outside the package. And..

Surely :roll: , Mr.Admin displayed some legendary patience in this thread. :lol:

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

Posted: Sun Sep 06, 2015 8:13 am
by nsobchuk
I've played a little with this question and I've found out something strange about this equals() overriding.
I've created two classes:

Code: Select all

public class Resource {
    private String data = "DATA";

    boolean equals(Resource r) { //LINE 1
        return (r != null && r.data.equals(this.data));
    }
}

Code: Select all

public class EqualsTest {
    public static void main(String[] args) {
        System.out.println(new Resource().equals(new Resource())); //LINE 2
    }
}
So forth, here's what I have found out:
1. If Both classes reside in the same package - equals(Resource r) of Resource class is called. I'm fine with that.
2. If classes reside in the different packages - Object's equals(Object o) method is called. I'm fine with that also.
3. If I change LINE 2 to be public boolean equals(Resource r) - it doesn't get called. That's not fine. Why wouldn't it? It's an exact match, more precise than equals(Object o) isn't it? Nevertheless, if I change LINE 2 to the following:

Code: Select all

System.out.println(new Resource().equals((Resource)new Resource())); //LINE 2
- public boolean equals(Resource r) get's called.

Could anyone explain me what is happening there? Why wouldn't equals match the exact match without an explicit cast?

UPD: This could be some IDEA related bug. I've tried playing some more with it and it behaves unpredictably :shock:

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

Posted: Sun Sep 06, 2015 8:45 am
by admin
If you make it public, it should indeed call Resource's equals method. Please double check what you are doing from command line instead of IDE.
-Paul.

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

Posted: Fri Jan 20, 2017 1:33 pm
by jagoneye
The king kong question of all! The assumption to be made in order to mark the third option was skyline! Well done whoever designed this question!
:thumbup:

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

Posted: Thu Mar 30, 2017 7:20 pm
by codingrobot
Yet another "why on earth" moment...

This and many other mock questions for the OCP exam have indeed very "illogical" answers. I can only appreciate that we have such resources available to prepare for the exam. The explanations and discussions on the forum are extremely useful! Excellent work Enthuware!

As the OCP Study Guide (Boyarsky, Selikoff) says - Don't do such things in code someone is paying you to write :mrgreen: