Multiple choice technology

Given the code String s1 = "yes"; String s2 = "yes"; String s3 = new String(s1); Which of the following would equate to true? (A) s1 == s2 (B) s1 = s2 (C) s3 == s1 (D) s1.equals(s2) (E) s3.equals(s1)

  1. (A), (C) & (E)

  2. (A), (B) & (C)

  3. (C), (D) & (E)

  4. A), (D) & (E)

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

String literals like 'yes' are interned, so s1 and s2 reference the same object; s1 == s2 is true (A). s3 uses new String(), creating a new object, so s3 == s1 is false (C). The equals() method compares content, so s1.equals(s2) (D) and s3.equals(s1) (E) are both true. Option B 's1 = s2' is assignment, not comparison. Option D correctly lists (A), (D), and (E).