aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Self test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Self test" Watch "K&B Self test" New topic
Author

K&B Self test

Ramesh Mangam
Greenhorn

Joined: May 03, 2005
Posts: 13
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

Joined: Sep 29, 2002
Posts: 1683
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?


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Ramesh Mangam
Greenhorn

Joined: May 03, 2005
Posts: 13
Oh! That's great. Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Self test
 
Similar Threads
Compilation fails
Kathy/Bert book question
hashCode() method !
hashCode()
varargs doubt