ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
String s = "level";
String t = s.replace('l', 'p');
System.out.print(t + " " + (s.replace('z', 'q') == s) + " " + s);
  1. A. pevep true level
  2. B. pevep false level
  3. C. pevel true level
  4. D. pevep true pevep

Correct answer: A. pevep true level

replace(char, char) replaces ALL occurrences and returns a new string, leaving s unchanged. When the old char does not occur, the specification says the same String object is returned, so == is true.

More OOP with C++ and Java questions

Exams that ask this topic