ProgrammingHardAccenture · Capgemini technical pattern
What is printed?
What is printed?
StringBuilder a = new StringBuilder("hi");
StringBuilder b = new StringBuilder("hi");
System.out.print(a.equals(b) + " " + a.toString().equals(b.toString()));- A. false true
- B. true true
- C. false false
- D. true false
Correct answer: A. false true
StringBuilder does not override equals, so it inherits Object.equals, which compares references: two different objects → false. Converting to String uses String.equals, which compares content → true.
More OOP with C++ and Java questions
- What happens when this Java code is compiled and run? java Object o = new ArrayList<String>(); List<String> l = (List<String>) o; String s…
- What is printed? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } A& operator=(const A&) { cout << "="; return this; } }…
- What is printed? java String s = "b"; switch (s) { case "a": System.out.print("A"); case "b": System.out.print("B"); case "c"…
- What happens when this Java code is compiled? java try { FileReader r = new FileReader("a.txt"); } catch (Exception e) {…
- This class is meant to be immutable. What change is still needed? java public final class Team { private final List<String> members…
- What is printed? cpp struct V { int x; V(int a) : x(a) {} V operator+(const V& o) const { return V(x + o.x 2); } }; int main() { V a(1)…