ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
String a = "hel" + "lo";
String b = "hello";
System.out.println(a == b);
  1. A. true
  2. B. false
  3. C. Compilation error
  4. D. Throws an exception

Correct answer: A. true

Both operands of + are compile-time string literal constants, so the compiler folds "hel" + "lo" into the single literal "hello" at compile time. a therefore refers to the same pooled literal as b, and a == b is true.

More OOP with C++ and Java questions

Exams that ask this topic