• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They DO ask these kind of questions, so be prepared!
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Obvious!
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do consider myself prepared... but I just don't know what I should respond to these questions...
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just in between learning sessions have a look at [url=http://en.wikipedia.org/wiki/Occam%27s_razor]Occam's razor[/url]
 
reply
    Bookmark Topic Watch Topic
  • New Topic