Multiple choice

What will be the output of the program? String s = "hello"; Object o = s; if( o.equals(s) ) { System.out.println("A"); } else { System.out.println("B"); } if( s.equals(o) ) { System.out.println("C"); } else { System.out.println("D"); }

  1. A
  2. B
  3. C
  4. D

  1. 1 and 3

  2. 2 and 4

  3. 3 and 4

  4. 1 and 2

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

In Java, String.equals() compares content. When o = s, o references the same String object. o.equals(s) returns true because String's equals() compares character sequences. s.equals(o) also returns true - Object equals() is virtual and String overrides it to compare content. Output is A and C.