ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
String s = "ab";
StringBuilder sb = new StringBuilder("ab");
System.out.print(s.equals(sb) + " " + s.contentEquals(sb) + " " + s.equals(sb.toString()));
  1. A. true true true
  2. B. false false true
  3. C. false true false
  4. D. false true true

Correct answer: D. false true true

String.equals returns true only for another String with the same characters; a StringBuilder is not a String. contentEquals compares against any CharSequence's characters.

More OOP with C++ and Java questions

Exams that ask this topic