ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
String s1 = "hi";
String s2 = new String("hi");
System.out.print((s1 == s2) + " " + s1.equals(s2));
  1. A. false true
  2. B. true true
  3. C. false false
  4. D. true false

Correct answer: A. false true

new String creates a separate object, so == (reference comparison) is false; equals compares contents and is true.

More OOP with C++ and Java questions

Exams that ask this topic