ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
static void m(int x) throws java.io.IOException, InterruptedException {
if (x == 0) throw new java.io.IOException();
else throw new InterruptedException();
}
public static void main(String[] a) {
try {
m(0);
} catch (java.io.IOException | InterruptedException e) {
System.out.print(e.getClass().getSimpleName());
}
}- A. Exception
- B. Compilation error: cannot combine unrelated exception types
- C. InterruptedException
- D. IOException
Correct answer: D. IOException
IOException and InterruptedException are unrelated but both checked, which multi-catch explicitly allows. m(0) throws an IOException, so the catch runs and e.getClass().getSimpleName() is "IOException".
More OOP with C++ and Java questions
- What happens when this runs, assuming recurse() causes infinite recursion? java static void recurse() { recurse(); } public static void…
- What is printed? java class AEx extends Exception {} class BEx extends AEx {} // in main, wrapped in throws Exception: try { throw new…
- Does this compile (Java 7+)? java static void m() throws java.io.IOException, java.sql.SQLException { try { if (Math.random() < 2) throw…
- Does this compile? java class A { void m() throws java.io.IOException {} } class B extends A { @Override void m() throws…
- What is printed, and what happens to the RuntimeException? java static int f() { try { throw new RuntimeException("x"); } finally { return…
- What is printed? java class MyEx extends RuntimeException { public String getMessage() { return "custom:" + super.getMessage(); } } public…