ProgrammingHardAccenture · Capgemini technical pattern

What is printed?

What is printed?
StringBuilder a = new StringBuilder("hi");
StringBuilder b = new StringBuilder("hi");
System.out.print(a.equals(b) + " " + a.toString().equals(b.toString()));
  1. A. false true
  2. B. true true
  3. C. false false
  4. D. true false

Correct answer: A. false true

StringBuilder does not override equals, so it inherits Object.equals, which compares references: two different objects → false. Converting to String uses String.equals, which compares content → true.

More OOP with C++ and Java questions

Exams that ask this topic