Java OCA OCP Practice Question 2410

Question

Select true statements about method hashCode().

  • a Classes HashSet and HashMap use method hashCode() to store and retrieve their values.
  • b Class TreeSet can use method hashCode() to store and retrieve its elements.
  • c Method hashCode() is used to test for object equality and inequality for a class.
  • d If hashCode() for two objects of the same class returns the same value, the objects are considered equal.
  • e For a class, method hashCode() can be used to test for object inequality.


a, e

Note

In option (a), classes HashSet and HashMap use hashing to store and retrieve their values.

Hashing uses the hash Code value to determine the bucket in which the values should be stored.

Option (b) is incorrect.

TreeSet ignores the hash Code values.

A TreeSet stores its elements based on its key's natural ordering or the ordering defined by a Comparator.

Options (c) and (d) are incorrect, and (e) is correct.

The hash Code value is used to test for object inequality.

If two objects return different hash Code values, they can never be equal.

But if your objects return the same hash Code values, they can be unequal (if their equals() returns false).




PreviousNext

Related