5.15 |
Which digits, and in which order, will be printed when the following program is run? public class MyClass { public static void main(String[] args) { int k=0; try { int i = 5/k; } catch (ArithmeticException e) { System.out.println("1"); } catch (RuntimeException e) { System.out.println("2"); return; } catch (Exception e) { System.out.println("3"); } finally { System.out.println("4"); } System.out.println("5"); } } Select the one correct answer.
|
5.16 |
Given the following program, which statements are true? public class Exceptions { public static void main(String[] args) { try { if (args.length == 0) return; System.out.println(args[0]); } finally { System.out.println("The end"); } } } Select the two correct answers.
|
5.17 |
What will be the result of attempting to compile and run the following program? public class MyClass { public static void main(String[] args) { RuntimeException re = null; throw re; } } Select the one correct answer.
|
5.18 |
Which statements are true? Select the two correct answers.
|
5.19 |
Which digits, and in which order, will be printed when the following program is compiled and run? public class MyClass { public static void main(String[] args) { try { f(); } catch (InterruptedException e) { System.out.println("1"); throw new RuntimeException(); } catch (RuntimeException e) { System.out.println("2"); return; } catch (Exception e) { System.out.println("3"); } finally { System.out.println("4"); } System.out.println("5"); } // InterruptedException is a direct subclass of Exception. static void f() throws InterruptedException { throw new InterruptedException("Time for lunch."); } } Select the one correct answer.
|
5.20 |
Which digits, and in which order, will be printed when the following program is run? public class MyClass { public static void main(String[] args) throws InterruptedException { try { f(); System.out.println("1"); } finally { System.out.println("2"); } System.out.println("3"); } // InterruptedException is a direct subclass of Exception. static void f() throws InterruptedException { throw new InterruptedException("Time to go home."); } } Select the one correct answer.
|
5.21 |
What is wrong with the following code? public class MyClass { public static void main(String[] args) throws A { try { f(); } finally { System.out.println("Done."); } catch (A e) { throw e; } } public static void f() throws B { throw new B(); } } class A extends Throwable {} class B extends A {} Select the one correct answer.
|
5.22 |
What is the minimal list of exception classes that the overriding method f() in the following code must declare in its throws clause before the code will compile correctly? class A { // InterruptedException is a direct subclass of Exception. void f() throws ArithmeticException, InterruptedException { div(5, 5); } int div(int i, int j) throws ArithmeticException { return i/j; } } public class MyClass extends A { void f() /* throws [...list of exceptions...] */ { try { div(5, 0); } catch (ArithmeticException e) { return; } throw new RuntimeException("ArithmeticException was expected."); } } Select the one correct answer.
|
5.23 |
What, if anything, would cause the following code not to compile? class A { void f() throws ArithmeticException { //... } } public class MyClass extends A { public static void main(String[] args) { A obj = new MyClass(); try { obj.f(); } catch (ArithmeticException e) { return; } catch (Exception e) { System.out.println(e); throw new RuntimeException("Something wrong here"); } } // InterruptedException is a direct subclass of Exception. void f() throws InterruptedException { //... } } Select the one correct answer.
|