ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
String s1 = "Java";
String s2 = "Ja" + "va";
String part = "Ja";
String s3 = part + "va";
System.out.print((s1 == s3) + " " + (s1 == s2) + " " + (s1 == s3.intern()));
  1. A. false false true
  2. B. false true true
  3. C. false false false
  4. D. true true true

Correct answer: B. false true true

"Ja" + "va" is a compile-time constant, folded into the pooled literal "Java", so s1 == s2. part is a variable, so part + "va" builds a new object at run time (false). intern() returns the pooled copy, which is s1.

More OOP with C++ and Java questions

Exams that ask this topic