ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
StringBuilder sb = new StringBuilder("abcdef");
sb.delete(1, 3);
sb.insert(2, "XY");
sb.reverse();
System.out.print(sb);- A. feYXda
- B. fedXYa
- C. feXYda
- D. fXYeda
Correct answer: A. feYXda
delete(1, 3) removes indices 1 and 2 (b, c), leaving adef. insert(2, "XY") gives adXYef, and reversing that gives feYXda.
More OOP with C++ and Java questions
- What is printed? java String s = "b"; int r = 0; switch (s) { case "a": r += 1; case "b": r += 2; case "c": r += 4; break; default: r +=…
- What is printed? java class R implements AutoCloseable { public void close() { throw new RuntimeException("C"); } } // in main: try { try…
- Which line fails to compile? java void run() { try { int x = 1; } catch (RuntimeException e) { } // line 1 try { int y = 2; } catch…
- What is printed? java class P { static { System.out.print("1"); } { System.out.print("2"); } P() { System.out.print("3"); } } class C…
- Which line fails to compile? java int base = 10; int step = 1; step = 2; Function<Integer, Integer> f = x -> x + base; // line 1…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(int... a) {…