ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
try {
try { System.out.print("1"); throw new Exception("a"); }
catch (Exception e) { System.out.print("2"); throw new RuntimeException("b"); }
finally { System.out.print("3"); }
} catch (RuntimeException e) { System.out.print("4" + e.getMessage()); }
finally { System.out.print("5"); }- A. 12345
- B. 1234b5
- C. 1234a5
- D. 123b45
Correct answer: B. 1234b5
The inner catch throws a new RuntimeException; the inner finally runs before it propagates (3), then the outer catch prints 4 with message b, and the outer finally prints 5.
More OOP with C++ and Java questions
- What happens when this is compiled (inside a method declared throws IOException)? java List<String> files = List.of("a.txt")…
- Which line fails to compile? java List<?> l = new ArrayList<String>(); l.add(null); // L1 l.add("x"); // L2 Object o = l.isEmpty() ? null…
- Which line fails to compile? java class Fruit implements Comparable<Fruit> { public int compareTo(Fruit o) { return 0; } } class Apple…
- Which line fails to compile? java // Java 16 or later Object o = "hello"; if (o instanceof String s && s.length() > 3)…
- What is printed? java Set<Short> s = new HashSet<>(); for (short i = 0; i < 5; i++) { s.add(i); s.remove(i - 1); }…
- What is printed? java Map<Long, String> m = new HashMap<>(); m.put(1L, "one"); System.out.print(m.get(1) + " " + m.get(1L) + " " +…