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);- A. pevep true level
- B. pevep false level
- C. pevel true level
- 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
- What is printed? java String s = "hello"; System.out.print("[" + s.substring(5) + "]"); System.out.print(s.substring(6));
- What is printed? java Integer a = 1000; Integer b = a; a++; System.out.print(a + " " + b + " " + (a == b));
- What is printed? java class P { int id; P(int id) { this.id = id; } public boolean equals(P o) { return o != null && id == o.id; } } // in…
- What is printed? java List<String> l = new ArrayList<>(List.of("pear", "fig", "apple", "kiwi", "date"))…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("Object"); } static void f(String s)…
- What is printed? java // static method of the main class: static void f(Object... a) { System.out.print(a == null ? "N " : a.length + "…