ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
List<String> l = new ArrayList<>(List.of("a", "b", "c"));
ListIterator<String> it = l.listIterator();
while (it.hasNext()) {
String s = it.next();
if (s.equals("b")) { it.set("B"); it.add("x"); }
}
System.out.print(l);- A. [a, x, B, c]
- B. [a, B, x, c]
- C. [a, B, c, x]
- D. A ConcurrentModificationException is thrown
Correct answer: B. [a, B, x, c]
set replaces the element last returned by next (b → B); add inserts at the cursor position, i.e. right after it and before c. Changes made through the iterator itself are allowed.
More OOP with C++ and Java questions
- What is printed? java System.out.print("a.b.c".split(".").length + " " + "a.b.c".split(".").length);
- What is printed? java System.out.print("a.b".replace(".", "x") + " " + "a.b".replaceAll(".", "x"));
- What is printed? java List<Integer> src = List.of(1, 2); List<Integer> dst = new ArrayList<>(5); Collections.copy(dst, src)…
- What is printed? java enum Size { S; public String toString() { return "small"; } } // in main: System.out.print(Size.S + " " +…
- What is printed? java Object o = new int[]{1}; System.out.print((o instanceof int[]) + " " + (o instanceof Object[]));
- Which line fails to compile? java class P { void run() throws java.io.IOException { } } class C extends P { @Override void run() { } } //…