ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement
What is printed?
What is printed?
List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4, 5)); l.subList(1, 3).clear(); System.out.print(l);
- A. [1, 4, 5]
- B. [1, 2, 3, 4, 5]
- C. [4, 5]
- D. [1, 5]
Correct answer: A. [1, 4, 5]
subList is a view of the backing list, so clearing indices 1 and 2 (elements 2 and 3) removes them from l itself. This is a standard idiom for removing a range.
More OOP with C++ and Java questions
- What is printed? java System.out.print("a,b,,c,,".split(",").length + " " + ",a".split(",").length + " " + "a,b,,c,,".split(","…
- What is printed? java StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1, 3); sb.insert(2, "XY"); sb.reverse()…
- 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…