About Question enthuware.ocpjp.v8.2.1297 :

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
mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

About Question enthuware.ocpjp.v8.2.1297 :

Post by mrmuiz »

Why is the option
If two distinct Resource objects are considered to be equal, then their 'data' fields must have contained the same string value.
considered correct?
I mean, that's just a common sense conclusion, but I'm not somehow forced to have this kind of behaviour.
I could have an additional field ID and then wanting two objects to be equals if their ID are the same, regardless of the "data" field.
Or I could just have

Code: Select all

	@Override
	public boolean equals(Object o){
		return false;
	}
That's not good practice, but not illegal. IMHO, a right answer would be
If two distinct Resource objects are considered to be equal, equals() method should return true.

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

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

Post by admin »

The options are talking about the code given in the question.
If you like our products and services, please help us by posting your review here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

That's exactly the point! Looking at the given code, "equals" method is not overridden, so no logic is defined for equality.

It's like saying that, given this class

Code: Select all

class A{
	int x;
}
two A objects, to be considered equal, must have the same x value. But who decided that? It would be the most reasonable choice to take, not the only possible, this is why using "must" in the answer looks too strong (if not wrong) to me.

Anyway, that's just a subtle consideration, the world won't collapse on intself for that =)

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

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

Post by admin »

You are interpreting the option incorrectly. Your example is completely different from the given situation.
The option starts with a precondition, "If two distinct Resource objects are considered to be equal, ". It does not say, "If two distinct Resource objects are to be considered equal..."
In other words, "If the call to the equals method does return true, ...". It is a given. In that case, "their 'data' fields must have contained the same string value." For the given code, this assertion is true.

Why do you think it is false? Can you show that data field does not contain the same string value and yet the equals method returns true?
-Paul.
If you like our products and services, please help us by posting your review here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

Uhm, at this point I don't know if that's me not being able to understand subtle differences of meaning, not being a native english speaker.

What you're saying is that if we accept that this sentence
If two distinct Resource objects are considered to be equal, their 'data' fields must have contained the same string value.
has the very same meaning of this one
If the call to the equals method does return true for two distinct Resource objects, their 'data' fields must have contained the same string value.
then it is true.

To be honest, I'm not so sure about this interpretation, but let's assume that's the right one.
Well, in this case this would be a tricky answer, and false too, in my opinion. It's tricky because, not being equals() overridden, two distinct Resource objects will be equal (according to Object.equals() implementation) only if they are.... well, the same object, not two distinct! In this case it is impossible for two distinct objects to be equal.

Furthermore, I think my example just simplify the one in the question: it's a class with an instance variable that does not ovveride Object.equals() method.

PS: please, I don't want to appear polemic or pedantic, it's just that I enjoy these kinds of discussions and exchange of opinions, and that's great having a forum like this linked to tests!

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

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

Post by admin »

mrmuiz wrote: Well, in this case this would be a tricky answer, and false too, in my opinion. It's tricky because, not being equals() overridden, two distinct Resource objects will be equal (according to Object.equals() implementation) only if they are.... well, the same object, not two distinct! In this case it is impossible for two distinct objects to be equals.
Not true. You can call equals method on two distinct Resource objects and still get true. It is possible with the given code. Please go through the explanation to know why.
Furthermore, I think my example just simplify the one in the question: it's a class with an instance variable that does not ovveride Object.equals() method.
Again, not true. While the class indeed does not override "the" equals method, that is not the only factor at play in the given code. The class does have one equals method, which is messing up the whole thing and that is what you are not considering.
PS: please, I don't want to appear polemic or pedantic, it's just that I enjoy these kinds of discussions and exchange of opinions, and that's great having a forum like this linked to tests!
You are right. That is why we have linked the forum to the questions and we don't shy away from a tough discussion. If you are prove us wrong, our product is improved and if we prove you wrong, we feel happy that we contributed at least something to your learning, which is the whole point :)

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

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

admin wrote: Not true. You can call equals method on two distinct Resource objects and still get true. It is possible with the given code. Please go through the explanation to know why.
I know, but is having

Code: Select all

new Resource().equals(new Resource())==true
sufficient to call two Resource objects equals?

Having this

Code: Select all

class Resource {
	private String data = "DATA";

	String getData() {return data;}

	void setData(String data) {this.data = data == null ? "" : data;}

	public boolean equals(Resource r){
		return (r != null && r.getData().equals(this.getData()));
	}
}
does lead to

Code: Select all

new Resource().equals(new Resource())==true
but also to

Code: Select all

		ArrayList<Resource> l = new ArrayList<>();
		l.add(new Resource());
		System.out.println(l.contains(new Resource()));  //prints false
just because the method that has been designed for equality has not been overridden, so the original one (Object.equals(Object obj)) is used to check for equality.

I think this sentence is badly formed and/or ambiguos
If two distinct Resource objects are considered to be equal...
What does it mean "considered"? And "considered" by whom? By the bad programmer who thought he had overridden the equals() method in the right way?

But, again, I'm just "splitting the hair in four" =)

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

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

Post by admin »

In what way can you consider two distinct objects to be equal other than by calling equals method on one and passing the other as the argument?
If you want test the equality of two distinct objects, you call the equals method. This is fairly standard wording. Which equals method will actually be invoked depends on your code. But the fact remains that when you talk about equality of two distinct objects, it is implies calling the equals method.

Whether the equals method has been overridden correctly or not and what are the implications of the given code is the subject matter of this particular question.
If you like our products and services, please help us by posting your review here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

I agree with every single word but these:
If you want test the equality of two distinct objects, you call the equals method. This is fairly standard wording.
Come on, let's just admit this sentence is a bit ambiguous
If two distinct Resource objects are considered to be equal [...]
we're talking about programming, I cannot "consider" something "equals" in a generic way, expecially in this context where equals method is being overloaded. Yes, it has to do with equals method, but I don't know how it's been called, there's not just one way. We're assuming we're calling equals by using a "Resource" reference, but

Code: Select all

		Object r2 = new Resource();
		System.out.println(r2.equals(new Resource()));
is not "true" anymore.

This would be a non-ambiguous version
if --new Resource().equals(new Resource())-- returns true [...]

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

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

Post by admin »

Your argument is incorrect. It doesn't matter what is the reference type. The same method will be called due to polymorphism. There is no ambiguity.
If you like our products and services, please help us by posting your review here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

Maybe I'm wrong and I haven't figured it out yet, but the reference type does matter, this:

Code: Select all

public class Main {

	public static void main(String[] args) {
		
		Resource r1 = new Resource();
		System.out.println(r1.equals(new Resource()));
		Object r2 = new Resource();
		System.out.println(r2.equals(new Resource()));
		
	}

}

class Resource {
	private String data = "DATA";

	String getData() {return data;}

	void setData(String data) {this.data = data == null ? "" : data;}

	public boolean equals(Resource r){
		return (r != null && r.getData().equals(this.getData()));
	}
}
outputs

Code: Select all

true
false
because Resource.equals() is invoked the first time, Object.equals() the second.

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

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

Post by admin »

You are right. I was wrong about the last statement (regarding type of reference). But that still does not prove the option statement incorrect because the first part of the statement stipulates that two distinct objects are considered equal. That can happen in only one case i.e. if the data member is equal.

To prove it wrong, you need to show a case where the data member is unequal and the call to equals (no matter how you call it) still returns true.

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

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

If two distinct Resource objects are considered to be equal, then their 'data' fields must have contained the same string value.
admin wrote:To prove it wrong, you need to show a case where the data member is unequal and the call to equals (no matter how you call it) still returns true.
Challenge accepted!

Code: Select all

		Resource r1= new Resource();
		r1.setData("_");
		Resource r2= new Resource(){public boolean equals(Resource r){return true;}};
		System.out.println(r2.equals(r1));
  1. r1 is a Resource object
  2. r2 is a distinct Resource object
  3. their data fields are different
  4. one of the possible equals methods is invoked
and yet true is returned.
This is obviously pushed to the extreme just to show how, in my opinion, an ambiguous statement like that can lead to paradoxical situations like this (all of the 4 premises are verified). Again, "[...] are considered to be equal [...]" to me is a way too broad definition. Again, my opinion can surely be wrong.

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

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

Post by admin »

The question is about the Resource class as given in the problem statement and the problem statement clearly asks you about the behavior of that class.
Identify the options that correctly describe the behavior of the above class.
The behavior that you have illustrated is not the behavior of the given class. It is the behavior of a class that extends the given class.

I understand your point and I agree that the question is borderline ambiguous. You are picking pieces of it and showing that it is ambiguous but if you take into account all the information that is given in the question, you will see that it does not really cross the border.

It will take me two seconds to update the question and avoid all this discussion but I truly feel that it is not ambiguous. Considering all the information given in the question, the option is very correct.

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

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

I'm not "questioning the question", that's not it being ambiguous to me.
I'm questioning the answer, that mentions "Resource objects".

I understand your point and I politely disagree =)
Still friends, though!

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

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

Post by admin »

1. Do you believe options are to be taken in isolation without the context provided by the problem statement?
2. What would be your suggestion for changing the option to make it unambiguous?

Friends of course. and thank you for your valuable feedback!
Paul.
If you like our products and services, please help us by posting your review here.

mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

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

Post by mrmuiz »

admin wrote:1. Do you believe options are to be taken in isolation without the context provided by the problem statement?
No, of course, the context is fundamental. But here there's no "context" really, it just says "here's a class, let's examine this class". If an answer deals with instances of the given class, well, I have to consider instances. We know how tricky these questions can be, best way to go is considering every possibility (is overthinking the best solution? Questionable but unfortunately legitimate in ambiguous contexts). I mean, if equality is checked via equals method (or == operator), instanceof operator doesn't lie either

Code: Select all

		Resource r2= new Resource(){public boolean equals(Resource r){return true;}};
		System.out.println(r2 instanceof Resource); //prints true
admin wrote:2. What would be your suggestion for changing the option to make it unambiguous?
Answers contain some code quite often. This could be the case:
If two distinct Resource objects are considered to be equal according to equals(Resource r) method of Resource class, then their 'data' fields must have contained the same string value.
or
If two distinct Resource objects
- Resource r1 = new Resource();
- Resource r2 = new Resource();
- r1.setData(/*someString*/);
- r2.setData(/*someOtherString*/);

are considered to be equal by the following instructions
- r1.equals(r2)
- ((Object)r1).equals(r2);

then their 'data' fields must have contained the same string value in both cases.
or something similar.

We know how crucial is having a good understanding of subtle differences of meaning: "legal" vs "valid", "does not compile" vs "does not run" etc... This is why i think "considered to be equal" is not 100% adequate. I've found some borderline ambiguous questions during the exams as well, I've never hated Oracle more =)

PS: I'm not asking for the answer to be changed, just discussing about it

Russtam
Posts: 9
Joined: Fri Dec 04, 2015 11:27 am
Location: Saint-Petersburg
Contact:

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

Post by Russtam »

Why option
The statement : new Resource().equals(new Resource()); will always return true.
is incorrect?
It always return true, because called overloaded method and both instances have data = "DATA"

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

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

Post by admin »

Did you read the explanation? It explains exactly what you are asking.
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 13 guests