ProgrammingVery hardProduct-company placement · GATE CS

What is printed?

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

Correct answer: C. true false true true

"ja" + "va" and e + "va" (e is a final String initialised with a constant) are constant expressions, so they are interned and share the literal's object. c + "va" is computed at run time and creates a new String, but intern() returns the pooled one.

More OOP with C++ and Java questions

Exams that ask this topic