About Question enthuware.ocpjp.v7.2.1297 :
Moderator: admin
-
- Posts: 14
- Joined: Thu Apr 18, 2013 2:05 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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
Reason: irrelevant comment removed
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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.
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.
-
- Posts: 53
- Joined: Fri Sep 20, 2013 7:20 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
"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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
Again, as mentioned in the explanation code that "calls" Resource.equals may not necessary invoke the equals method that you think would be invoked.Student wrote: Certainly any code that uses Resource.equals will consider two objects with the same data values as equal.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Thu Jan 29, 2015 4:49 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
Best question so far!
-
- Posts: 8
- Joined: Wed Dec 10, 2014 6:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
Nice question and even best explanation. Very very tricky.
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
"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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
Sorry, I am unable to understand what you mean.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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 wrote:Sorry, I am unable to understand what you mean.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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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.
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.
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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.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.
"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"
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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.
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.
-
- Posts: 21
- Joined: Thu Jul 16, 2015 4:22 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
Ah ok, it does say 2 "distinct" objects. I understand. thxadmin 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.
-
- Posts: 15
- Joined: Fri May 15, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
Very good question. Didn't consider what would happen if you call equals from outside the package. And..
Surely , Mr.Admin displayed some legendary patience in this thread.
Surely , Mr.Admin displayed some legendary patience in this thread.
-
- Posts: 3
- Joined: Sun Aug 17, 2014 7:59 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
I've played a little with this question and I've found out something strange about this equals() overriding.
I've created two classes:
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:
- 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
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
}
}
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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!
-
- Posts: 1
- Joined: Thu Mar 30, 2017 7:10 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1297 :
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
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
Who is online
Users browsing this forum: Google [Bot] and 74 guests