ProgrammingHardWipro Elite · TCS NQT Prime

What is printed?

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

Correct answer: D. false true true

new String creates a separate heap object; intern() returns the pooled instance, which is the same object as the literal a.

More OOP with C++ and Java questions

Exams that ask this topic