Java OCA OCP Practice Question 474

Question

Fill in the blanks:

Given two non-null String objects with reference names apples and oranges,

if apples ____ oranges evaluates to true, then apples ____ oranges must also evaluate to true.

  • A. ==, equals()
  • B. !=, equals()
  • C. equals(), ==
  • D. equals(), =!


A.

Note

If two String objects evaluate to true using ==, then they are the same object.

If they are the same String object, equals() will trivially return true.

Option A correctly reflects this principle.

Option B is incorrect as two String objects that are not the same may still be equivalent in terms of equals().

For example, apples == new String(apples) evaluates to false, but equals() will evaluate to true on these String objects.

Options C and D are also incorrect because two String objects that are equivalent in terms of equals() may be different objects.




PreviousNext

Related