ProgrammingHardGATE CS · TCS Digital · Advanced

What is printed?

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

Correct answer: A. false true true

b is not a constant variable, so b + "b" is built at run time as a new object (a == c is false). d is a final constant, so d + "b" is folded at compile time to the pooled "ab"; intern() returns the pooled instance.

More OOP with C++ and Java questions

Exams that ask this topic