ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
StringBuilder sb = new StringBuilder("abcdef");
sb.insert(2, "XY").delete(5, 7).reverse().setCharAt(0, 'Z');
System.out.print(sb);- A. ZYXba
- B. ZcYXba
- C. ZcXYba
- D. ZecYXba
Correct answer: B. ZcYXba
insert gives abXYcdef; delete(5, 7) removes indices 5 and 6 (d, e) → abXYcf; reverse → fcYXba; setCharAt(0, 'Z') → ZcYXba. All of these mutate the same builder.
More OOP with C++ and Java questions
- What is printed? java String s = "level"; String t = s.replace('l', 'p'); System.out.print(t + " " + (s.replace('z', 'q') == s) + " " + s);
- 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)…