Java OCA OCP Practice Question 393

Question

Consider :

o1 and o2 denote two object references to two different objects of same class.

Which of the following statements are true?

Select 2 options

  • A. o1.equals (o2) will always be false.
  • B. o1.hashCode() == o2.hashCode() will always be false.
  • C. o1 == o2 will always be false.
  • D. Nothing can be said about o1.equals(o2) regarding what it will return based on the given information.
  • E. Nothing can be said about o1 == o2.


Correct Options are  : C D

Note

A. is wrong.

It depends on how the equals method is overridden.

If it is not overridden, then it will return false.

B. is wrong. hashCode() can be overridden and so the given statements is not true.

C. is a correct answer.

The == operator compares whether the two references are pointing to the same object or not.

Here, they are not, so it returns false.

D. is a correct answer.

It depends on how the class implements this method.

E. is wrong.

It will always return false if references are to two different objects.

Both equals () and hashCode() methods can be overridden by the programmer so you can't say anything about what they will return without looking at the code.




PreviousNext

Related