About Question enthuware.ocpjp.v7.2.1297 :

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

Moderator: admin

Post Reply
arnoldnitesh
Posts: 14
Joined: Thu Apr 18, 2013 2:05 pm
Contact:

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

Post 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.
Last edited by admin on Wed Sep 25, 2013 7:47 am, edited 1 time in total.
Reason: irrelevant comment removed

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Student
Posts: 53
Joined: Fri Sep 20, 2013 7:20 am
Contact:

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

Post 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 :)

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post by romsky »

Best question so far!

jmrego
Posts: 8
Joined: Wed Dec 10, 2014 6:05 am
Contact:

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

Post by jmrego »

Nice question and even best explanation. Very very tricky.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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"

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

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

Post 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

Ad9999
Posts: 15
Joined: Fri May 15, 2015 12:06 am
Contact:

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

Post 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:

nsobchuk
Posts: 3
Joined: Sun Aug 17, 2014 7:59 am
Contact:

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

Post 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:

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post 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:

codingrobot
Posts: 1
Joined: Thu Mar 30, 2017 7:10 pm
Contact:

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

Post 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:

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests