aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with K&B Learnkey MasterExam question about hashCode & equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with K&B Learnkey MasterExam question about hashCode & equals" Watch "problem with K&B Learnkey MasterExam question about hashCode & equals" New topic
Author

problem with K&B Learnkey MasterExam question about hashCode & equals

Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

I can't give the full question, because it's in the exam engine and I can't copy it, but basically, to summarize:

"if x5.equals(x6) == true, which of the following statements will always be true?"


Correct answer:

C: x5.hashCode() == x6.hashCode();

Explanation: By contract, if .....



...What did I miss? By contract? That wasn't in the question... When the equals method returns true, you have no idea whether hashCode will return true, unless you have a guarantee that the developer who implemented the class took the time to fulfill the equals and hashCode contracts. For all I know, equals() could've returned true and hashCode could've returned (int)(Math.random()*10000).


Will they ask questions like this on the exam? How do I know what the answer is? There are many questions like this, where the answer is ambiguous and I'm not sure what to answer.

Like another question on the same exam asked "What will NOT be in the output of this program?" and then a list of options, and after that "will not compile" and "runtimeexception". I have no idea if I should cross that they will not be in the output, or crossing them will actually mean I found it to be a compile time error or runtime exception.... I'm confused...


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Have a look a the contract

It says
If two objects are equal according to the equals(Object) method, then calling
the hashCode method on each of the two objects must produce the same
integer result.

which is one of the rules one has to comply with in order to write a correct equals() method.
Therefore if two objects return different hashCodes they cannot be equal.

Of course the default equals() and hashCode() fullfil all these rules.


"Eppur si muove!"
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

They DO ask these kind of questions, so be prepared!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Another one... paraphrased from the LearnKey MasterExam:

IS-A relationships always require at least two class types, and always rely on polymorphism


So, "Integer IS-A Integer" is not a correct statement?
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Gian Franco wrote:Have a look a the contract

It says
If two objects are equal according to the equals(Object) method, then calling
the hashCode method on each of the two objects must produce the same
integer result.

which is one of the rules one has to comply with in order to write a correct equals() method.
Therefore if two objects return different hashCodes they cannot be equal.

Of course the default equals() and hashCode() fullfil all these rules.

Yes, but the question did not ask for the correct usage... It asked what would always be true. I interject, that not always will this be true, in fact, it will only be true if the developer stuck to the contract. Developers don't always stick to the contract...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Dieter Quickfend wrote:
So, "Integer IS-A Integer" is not a correct statement?


Obvious!
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

I do consider myself prepared... but I just don't know what I should respond to these questions...
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Dieter Quickfend wrote:I can't give the full question, because it's in the exam engine and I can't copy it, but basically, to summarize:

"if x5.equals(x6) == true, which of the following statements will always be true?"


Correct answer:

C: x5.hashCode() == x6.hashCode();

Explanation: By contract, if .....



...What did I miss? By contract? That wasn't in the question...

I guess when nothing explicit is mentioned, you have to assume ground rules, if you go by Developers' rules there can be thousand different answers
Nevertheless, I fully agree with you that fewquestions in LearnKey are confusing.
One that I remember was something like "House has roof, and turn on Stereo, house has doors". This question can also have different answers


Cheers,
Divyesh.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Dieter,

Just FYI, it's fine to take a screenshot of a mock question and post the screenshot here, as long as you mention where the question came from.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Dieter,

I think when the information is not sufficient then to make an assumption we can stick with the ground rules.
( As sugested by Divyeshh )


Regards,
Avishkar Nikale
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
One more answer I came across (don't remember where) was "it is appropriate to throw AssertionError in the program"(the word "explicit" was not mentioned explicitly in the question)
But what I believe is, putting an assert statement in the program (though it might always be evaluated to false) is different from throwing an AssertionError in the program
Hope what I wrote is not too confusing...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Yes it is always appropriate a AssertionError to throw .
we can catch it because it is sublcass of Error and Error is a subclass of Throwable..it is not recommended to catch the assertion error..
if you really wanna catch the error why not you used if-else block irrespective of assertion...

SCJP6.0,My blog Ranchers from Delhi
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
This question is from Apress book for SCJP, chapter 8, Question 6

Consider the following code:
1. FileOutputStream out=new FileOutputStream("objectStore.ser");
2. ObjectOutputStream os = new ObjectOutputStream(out);
3. os.writeObject("Object on the fly!");
What is the result of this code?
A. The string "Object on the fly!" is written into the file objectStore.ser.
B. Line 3 causes a compiler error.
C. The code compiles fine but an exception is thrown at line 3 when the code is executed.
D. An exception occurs at line 2.
E. A compiler error occurs at line 2.

I assumed that the answer would be compiler error at line 1 which does not appear in the option list, but they wanted me to assume that the code is enclosed in try-catch...
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Divyeshh Patel wrote:This question is from Apress book for SCJP, chapter 8, Question 6

Consider the following code:
1. FileOutputStream out=new FileOutputStream("objectStore.ser");
2. ObjectOutputStream os = new ObjectOutputStream(out);
3. os.writeObject("Object on the fly!");
What is the result of this code?
A. The string "Object on the fly!" is written into the file objectStore.ser.
B. Line 3 causes a compiler error.
C. The code compiles fine but an exception is thrown at line 3 when the code is executed.
D. An exception occurs at line 2.
E. A compiler error occurs at line 2.

I assumed that the answer would be compiler error at line 1 which does not appear in the option list, but they wanted me to assume that the code is enclosed in try-catch...

if assumtion for tyr/catch is not made then it should be compiler-error....
otherwise everthing is fine....
A. The string "Object on the fly!" is written into the file objectStore.ser.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

serialization is not on the exam now.....
donot concentarte much on this from the exam prospective...

but you should know the basics
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

It could also be compiler error because they donot give the assumption for import statement...
and code given by you requires following

statement........
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Shanky Sohar wrote:It could also be compiler error because they donot give the assumption for import statement...
and code given by you requires following

statement........

You are right, even the import is missing and nothing is mentioned about import assumption.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

Bert Bates wrote:Hi Dieter,

Just FYI, it's fine to take a screenshot of a mock question and post the screenshot here, as long as you mention where the question came from.

hth,

Bert

You're right Bert, I should've thought of that, I've been a little fatigued, busy job and studying for the exam doesn't leave me in the best state of mind on week nights. I hope I don't come off too harsh, the book you wrote with Kathy Sierra is the main reason I'm already attempting this exam, so I owe you my sincerest thanks; I'm just a little stressed out with the exam coming up, and I want to make sure I can get as much of the answers as possible.

Abimaran Kugathasan wrote:
Dieter Quickfend wrote:
So, "Integer IS-A Integer" is not a correct statement?


Obvious!

Well, the answer didn't agree with you, as there is only one class type there and it's final, so there's no polymorphism involved.

I'm fine with using the ground rules if it's ambiguous, but I've read no book that states you ALWAYS need two classes to have an IS-A, and you ALWAYS rely on polymorphism, and that's what the question says, so to me, the (apparently correct) answer is obviously false.

Same with hashCode() and equals(). Had the question been "what would always be true, given appropriate overriding of equals and hashCode", I would've given the right answer, but the question says "what will always be true", and it won't, because if I make my own class with an inappropriate hashCode/equals contract, "always" is out the door.

I doubt I'll be given the chance to explain my answer on the exam, and I doubt there will be several correct answers to a radio-button question, so I'm asking if there is any way to know what they mean when they ask such questions? A way to know if I should be specific or generalize, if questions give me a choice.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
just in between learning sessions have a look at [url=http://en.wikipedia.org/wiki/Occam's_razor]Occam's razor[/url]
 
jQuery in Action, 2nd edition
 
subject: problem with K&B Learnkey MasterExam question about hashCode & equals