• 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

Kathy/Bert book question

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second question on page 447 , I thought the answer is E but the book says it is C.
In the class Test2 also the hashcode() method is returning the same constant everytime , because the declared int value has default value 0.That is the reason I tought it is E.
Am I missing anything?
Archana
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be very happy if I could see the question, even if I cannot answer it!! Please, post it!!
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question is
class Test1{
public int value;
public int hasCode(){ return 42;}
}
class Test2
{
public int value;
public int hashcode(){ return (int)(value^5);}
}

Which statement is true?
A.Class Test1 will not compile
B.The Test1 hascode() method is more efficent than the Test2 hascode()
C.The Test1 hashcode() method is less efficient than Test2 hascode()
D.Class Test2 will not compile
E.The two hashcode() methods will have the same efficiency
The book answer is C , I thought it is E.
Archana
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Archana,
Your answer seems to be correct. While the hashcode is returning a hard coded 42 in first case it is returning 0^5 which is also does not change depeding up on the other instance variables as in this there are none.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Archana,
Here is my interpretation of the question (or rather the answer in the book)
The hascode() method in Test1 returns a constant('42') for all invocations.
The hascode() method in Test2 uses the variable 'value' in the calculations. This method returns different values depending on the value of the instance varialble 'value' of the Test2 object on which the hashcode() method is invoked.
Your assumption that of 'value' being zero is not valid here as it could be changed (could be anything between 2^31 and 2^31 - 1) by the time the hascode() method of a Test2 object is invoked.
This makes the 'C' the correct answer for the question.

Hope this clears it up...

-- Vikram
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikram,
I agree with you, if we interpret the question like that.
I felt we don't assume anything in the exam.
I hope the questions in the exam will be more clear.
Archana
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikram's answer is correct...
(although there is a new errata because of a capitalization problem :roll: )
The discussion about the vagueness of the questions is a tough one. Sun's perspective is that you should assume that things are 'normal' about the world in which these questions reside... (You know, you've got the right compiler, enough memory, etc.)
So in this case class Test2's instance variable is public so it's not at all hard to imagine that if you were hashing multiple instances of this class, some of these instances would have had their instance variable's value changed...
I'm afraid you'll find that many of the questions on the real exam are very terse... I think this question is REALLY in the spirit of what you'll find on the real exam...

-Bert
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the thing that i dont understand yet ... is that value, a public int, might be changed at any time! yet the hashcode contract calls for consistency.... i.e. "Multiple calls to x.hashCode() return the same integer" (p.444, Kathy_Bert_Book).
?
 
Vikram Reddy
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper,
The statement "Multiple calls to x.hashCode() return the same integer" is half the story.
The complete statement should look something like this "Multiple calls to x.hashCode() return the same integer when the variables used in the equals operation for the object remain unchanged"
In effect what the contract says is "As long as the values of variables used in the equals() method are not changed, the hashcode() method should return the same integer value for all calls on the object".
-- Vikram
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Bert for the explanation, I am afraid too(It seems i was thinking little extra).

Anyway I will make sure myself that I don't get mislead.
Archana
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikram Reddy:

In effect what the contract says is "As long as the values of variables used in the equals() method are not changed, the hashcode() method should return the same integer value for all calls on the object".


aha, good call Vikram, thanks indeed for the clarification!
reply
    Bookmark Topic Watch Topic
  • New Topic