ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
String a = "Hi";
String b = new String("Hi");
String c = b.intern();
StringBuilder d = new StringBuilder("Hi");
System.out.print((a == b) + " " + (a == c) + " " + a.equals(d) + " " + a.contentEquals(d));
  1. A. false true true true
  2. B. false true false true
  3. C. true true false true
  4. D. false false false true

Correct answer: B. false true false true

new String creates a distinct object (false), and intern() returns the pooled literal (true). String.equals is true only for another String, so comparing with a StringBuilder gives false; contentEquals compares characters (true).

More OOP with C++ and Java questions

Exams that ask this topic