ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

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

Correct answer: D. false true true

x is not a constant, so x + "b" builds a new String at run time (false). y is a final variable initialised with a constant, so y + "b" is a compile-time constant and is interned like the literal (true). intern() returns the pooled "ab" (true).

More OOP with C++ and Java questions

Exams that ask this topic