ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
// in main:
String a = "Hello".toLowerCase();
String b = "hello";
System.out.println((a == b) + " " + a.equals(b));
  1. A. true true
  2. B. false true
  3. C. true false
  4. D. false false

Correct answer: B. false true

toLowerCase() always returns a newly constructed String object (never the pool entry, even if the content happens to match a literal), so a == b is false, but the character content is identical, so equals() is true.

More OOP with C++ and Java questions

Exams that ask this topic