QuestionQ157

Using Operators and Decision Constructs

Given:

Question Image

and this code fragment:

Question Image

What is the output?

  • A AC
  • B BD
  • C BC
  • D AD
Explanation

equals(OraString) overloads Object.equals(Object) because its parameter type differs. Calling s2.equals("Moon") therefore resolves to the inherited equals(Object) method and performs reference equality, which is false for an OraString object and a String. The two identical string literals in s1 == "Moon" refer to the same interned object, so the first branch prints B. The field s2.s contains "Moon", making s1.equalsIgnoreCase(s2.s) true and causing C to print.

Community Discussion

No comments yet. Be the first to start the discussion!