Java OCA OCP Practice Question 2404

Question

Which statements are true about method hashCode()?.

  • a Method hashCode() is used by classes such as HashMap to determine inequality of objects.
  • b Method hashCode() is used by classes such as HashSet to determine equality of objects.
  • c Method hashCode() is used by class Collections.sort to order the elements of a collection.
  • d Method hashCode() is used by classes like HashSet, TreeSet, and HashMap, which use hashing to group their elements into hash buckets.
  • e An efficient hashCode() method includes use of a particular algorithm recommended by Java.


a

Note

Option (b) is incorrect.

Method equals()is used to determine the equality of objects.

Option (c) is incorrect.

Class Collections defines two overloaded versions of method sort().

Both accept a List object, with or without a Comparator object.

Method sort() sorts a List passed to it into ascending order, according to the natural ordering of its elements, or by using the order specified by a Comparator object.

Option (d) is incorrect.

Though HashSet and HashMap use hashCode() for hashing, TreeSet doesn't.

Option (e) is incorrect.

Java doesn't recommend any particular algorithm for writing an efficient hashCode() method.

But Java does recommend writing an efficient algorithm.




PreviousNext

Related