aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Kathy/Bert book question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Kathy/Bert book question" Watch "Kathy/Bert book question" New topic
Author

Kathy/Bert book question

Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
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
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
I would be very happy if I could see the question, even if I cannot answer it!! Please, post it!!
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
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
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
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.
Vikram Reddy
Greenhorn

Joined: Jan 06, 2003
Posts: 17
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


SCJP-1.4; SCWCD-EE5<br />---------<br />Sky is not the limit......
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
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
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
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


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

Joined: Jan 10, 2003
Posts: 284
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).
?


giddee up
Vikram Reddy
Greenhorn

Joined: Jan 06, 2003
Posts: 17
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

Joined: Jan 29, 2003
Posts: 147
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

Joined: Jan 10, 2003
Posts: 284
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Kathy/Bert book question
 
Similar Threads
Jsp [HFSJ Pg330 ,q6]
Question about generics
getValue - returns new / old ?
Differentiate Custom Tags and JSTL - HF Chapter 9 Mock Exam
book on Java performance tuning