ProgrammingModerateTCS NQT · Infosys pattern

What is printed?

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

Correct answer: D. true false true

Equal string literals share one object from the string pool, so a == b is true. new String creates a separate object, so a == c compares different references (false). equals compares characters (true).

More OOP with C++ and Java questions

Exams that ask this topic