Hashcodes Question 1 Suppose that an instance of class C has legal implementations of the hashCode and equals methods. Within any one execution of the Java application, the hash code contract requires that each invocation of the hashCode method on the same instance of class C must consistently return the same result as long as the fields used for the equals comparison remain unchanged. a. false b. true ANSWER 1 b true
Question 2 If two instances of a class type are equal according to the equals method, then the same integer value must be returned by the hashCode method of the two objects. a. false b. true ANSWER If two objects are equal according to the equals method, then the hashcodes produced by the hashCode method must also be equal. If two objects are not equal according to the equals method, then the hashcodes 2 b true may or may not be equal. It is preferable that unequal objects have different hashcodes, but that is not always possible. Since the hash code value is a 32 bit primitive int, it is not possible to produce a unique hash code for each value of a primitive long.
Question 3 If two instances of a class type are not equal according to the equals method, then the same integer value must not be returned by the hashCode method of the two objects. a. false b. true
ANSWER If two objects are equal according to the equals method, then the hashcodes must also be equal. If two objects are not equal according to the equals method, then the hashcodes may or may not be equal. It is 3 a false preferable that unequal objects have different hashcodes, but that is not always possible. Since the hash code value is a 32 bit primitive int, it is not possible to produce a unique hash code for each value of a primitive long.
Question 4 class A { static void m1 (B a, B b, B c, B d, B e, B f, B g, B h) { if (a.equals(b)) {System.out.print("A");} if (!c.equals(d)) {System.out.print("B");} if (e.hashCode() == f.hashCode()) {System.out.print("C");} if (g.hashCode() != h.hashCode()) {System.out.print("D");} }}
Suppose that method m1 is invoked with eight instances of the same class and the output is ABCD. If the B.equals and B.hashCode methods are implemented according to the hash code contract, then which of the following statements must always be true? a. b. c. d.
(a.hashCode() == b.hashCode()) (c.hashCode() != d.hashCode()) (e.equals(f)) (!g.equals(h)) ANSWER
(a.hashCode() == a b.hashCode()) (! 4 d g.equals(h))
If two objects are equal according to the equals method, then the hashcodes must also be equal. If two objects are not equal according to the equals method, then the hashcodes may or may not be equal. If two objects have the same hash code, then the objects may or may not be equal. If two objects have different hashcodes, then the objects must not be equal.
Question 5 class B { private int i1; public int hashCode() {return 1;} } class C {
private int i1; public int hashCode() {return -1;} } class D { private int i1; public int hashCode() {return i1;} }
Suppose that the equals method of classes B, C and D all make use of the value of the int variable, i1. Which class has a hashCode method that is not consistent with the hash code contract? a. b. c. d.
B C D None of the above ANSWER Suppose that the hashCode method is invoked on the same object more than once during the same execution of a Java application. If no information used in the equals comparison is modified, then each invocation of the hashCode method must produce the same hash code None of value. The hashCode methods of classes B and C will always return 5 d the the same value during an execution of the Java application and are above therefore consistent with the hash code contract. Even so, the hashCode methods of classes B and C are not efficient, because they will cause hashtables to place every instance of classes B and C in the same bucket. The hashCode method of class D is appropriate and will allow a hash table to operate efficiently.
Question 6 Which of the following classes override both the equals and hashCode methods? a. b. c. d. e.
java.lang.Byte java.lang.Integer java.util.Vector java.lang.String java.lang.StringBuffer ANSWER 6 a java.lang.Byte b java.lang.Integer c java.util.Vector
The wrapper classes and the collection classes override the equals and hashCode methods. The String class
d java.lang.String
overrides the equals and hashCode methods, but StringBuffer does not.
Question 7 class A { int i1, i2; public void setI1(int i) {i1 = I;} public int getI1() {return i1;} public void setI2(int i) {i2 = I;} public int getI2() {return i2;} public A(int ii1, int ii2) {i1 = ii1; i2 = ii2;} public 4oolean equals(Object obj) { if (obj instanceof A) { return (i1 == ((A)obj).getI1()); } return false; } public int hashCode() { // Insert statement here. }}
Which of the following statements could be inserted at the specified location without violating the hash code contract? a. b. c. d.
return 31; return getI1(); return getI2(); return 31 * getI1() + getI2(); ANSWER
7
return 31; a return b getI1();
Question 8
A hashCode method that returns the constant value 31 is consistent with the hash code contract. Even so, a hashCode method that returns the same value regardless of the internal state of the object is not very good, because it will cause hashtables to place every instance of the class in the same bucket. If the equals method determines that two instances are equal, then the two instances must produce the same hash code. For that reason, the hash code must not be calculated using fields that are not used to determine equality. In this case, the equals method determines equality based on the value of i1. The value of i2 is not used to determine equality; therefore, i2 can not be used to calculate the hash code.
class A { int i1, i2; public void setI1(int i) {i1 = i;} public int getI1() {return i1;} public void setI2(int i) {i2 = i;} public int getI2() {return i2;} public A(int ii1, int ii2) {i1 = ii1; i2 = ii2;} public boolean equals(Object obj) { if (obj instanceof A) { return (i1 == ((A)obj).getI1()) & (i2 == ((A)obj).getI2()); } return false; } public int hashCode() { // Insert statement here. }}
If inserted at the specified location, which of the following statements would produce the most efficient hashCode method? a. b. c. d. e. f.
return 31; return getI1(); return getI2(); return getI1() + getI2(); return 31 * getI1() + getI2(); None of the above ANSWER All of the statements would produce a hashCode method that is consistent with the hash code contract. The expression 31 * getI1() + getI2() produces the most efficient return 31 * hashCode method, because it is most likely to produce 8 e getI1() + unique hashcodes for various combinations of i1 and i2. The getI2(); expression getI1() + getI2() is less efficient, because it produces the same hash code when the values of i1 and i2 are swapped.