File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Self test

 
Ramesh Mangam
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOTE: For those who have not taken the SELF TEST on pg 447, this discussion will reveal the answer.
___________________________________________
K&B Pg 447, Ques 2:

Given the following:
class Test1 {
public int value;
public int hashCode() {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 hashCode() method is more efficient than Test2 hashCode() method.
C. The Test1 hashCode() method is less efficient than Test2 hashCode() method.
D. class Test2 will not compile
E. The two hashCode() methods will have the same efficiency.

The correct answer, according to K&B, is C as Test1 hashCode() method always returns the same value. I feel the correct answer is E because even the Test2 hashCode() method will always return the same value. The instance variable 'value' of Test2 will be given the default value '0' for all instances and there are no other setter methods that could change its value. So, (int) (value^5) will always return the same value.

Where am I going wrong?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance variable 'value' of Test2 will be given the default value '0' for all instances and there are no other setter methods that could change its value. So, (int) (value^5) will always return the same value.

Where am I going wrong?

Look at the access modifier of the value variable. Can another class with a reference to an instance of Test2 directly change value?
 
Ramesh Mangam
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! That's great. Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic