79.2k views
3 votes
Select all of the true statements about java root class object:

a. it has the hashcode method, which returns an integer hash code
b. during the execution of a program, invoking the hashcode method multiple times returns the same integer, provided that the object's data are not changed
c. you should override the hashcode method whenever the equals method is overridden to ensure that two equal objects return the same hash code
d. two unequal objects may have the same hash code, but you should implement the hashcode method to avoid too many such cases
e. by default, the hashcode method returns the memory address for the object

User Arshid Dar
by
7.7k points

1 Answer

3 votes

Final answer:

The correct answer is option C. The hashCode method should be overridden whenever the equals method is overridden to ensure that two equal objects return the same hash code.

Step-by-step explanation:

The correct answer is option C. When the equals method is overridden, it is recommended to also override the hashCode method to ensure that two equal objects return the same hash code. This is because the equals method checks for equality of objects based on their content, while the hashCode method generates a unique integer value for each object.

Option A is incorrect because not all root class objects in Java have the hashCode method. Option B is also incorrect because invoking the hashCode method multiple times on the same object may return different integers if the object's data are changed. Option D is incorrect because implementing the hashCode method does not guarantee that two unequal objects will never have the same hash code.

Option E is also incorrect because by default, the hashCode method in Java does not return the memory address for the object, but rather a unique integer calculated based on the object's content.