ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
int i = 0;
while (true) {
try { i++; if (i == 3) break; }
finally { System.out.print(i); }
}- A. 123
- B. 12
- C. 1233
- D. Infinite loop
Correct answer: A. 123
finally runs on every iteration, including the one that leaves the loop with break, so 1, 2 and 3 are all printed.
More OOP with C++ and Java questions
- What is printed? java String s = null; switch (s) { case "a": System.out.print("A"); break; default: System.out.print("D"); }
- What is printed? java class Box<T> { static int count = 0; Box() { count++; } } // in main: new Box<String>(); new Box<Integer>(); new…
- Which lines fail to compile? java class G<T> { T[] arr = new T[10]; // L1 T make() { return new T(); } // L2 boolean is(Object o) { return…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3)); Iterator<Integer> it = l.iterator(); it.next(); it.remove()…
- What is printed? java Map<String, Integer> m = new LinkedHashMap<>(16, 0.75f, true); m.put("a", 1); m.put("b", 2); m.put("c", 3)…
- What is printed? java Map<String, Integer> m = new LinkedHashMap<>(); m.put("x", 1); m.put("y", 2); m.put("x", 3); System.out.print(m);