Java OCA OCP Practice Question 3141

Question

Given that the objects referenced by the parameters override the equals() and the hashCode() methods appropriately, which return values are possible from the following method?

String func(Object x, Object y) {
    return (x == y) + " " + x.equals(y) + " " + (x.hashCode() == y.hashCode());
}

Select the four correct answers.

  • (a) "false false false"
  • (b) "false false true"
  • (c) "false true false"
  • (d) "false true true"
  • (e) "true false false"
  • (f) "true false true"
  • (g) "true true false"
  • (h) "true true true"


(a), (b), (d), and (h)

Note

(c) is eliminated since the hashCode() method cannot claim inequality if the equals() method claims equality.

(e) and (f) are eliminated since the equals() method must be reflexive, and (g) is eliminated since the hashCode() method must consistently return the same hash value during the execution.




PreviousNext

Related