ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
class AppEx extends Exception { }
class DbEx extends AppEx { }
// static method of the main class:
static void run() throws AppEx { throw new DbEx(); }
// in main:
try { run(); }
catch (DbEx e) { System.out.print("Db"); }
catch (AppEx e) { System.out.print("App"); }- A. Db
- B. App
- C. DbApp
- D. Compile-time error: run() does not declare DbEx
Correct answer: A. Db
The throws clause names a supertype, but handlers are matched against the actual runtime class, a DbEx, so the first (more specific) handler runs.
More OOP with C++ and Java questions
- What is printed? java // in main, which is declared throws Exception: try (AutoCloseable r = null) { System.out.print("body"); }…
- How many of these statements are correct in Java? (i) A subclass object contains storage for its superclass's private fields. (ii) A…
- What is printed? java int[][] a = {{1}, {2}}; int[][] b = a.clone(); b[0][0] = 9; b[1] = new int[]{5}; System.out.print(a[0][0] + " " +…
- What is printed? java StringBuilder[] sbs = new StringBuilder[3]; Arrays.fill(sbs, new StringBuilder()); sbs[0].append("x")…
- What is printed? java List<Integer> l = new ArrayList<>(10); System.out.print(l.size() + " "); l.set(0, 1); System.out.print(l);
- What is printed? java List<Integer> l = new ArrayList<>(List.of(2, 2, 3, 2, 4)); for (int i = 0; i < l.size(); i++) if (l.get(i) == 2)…