Review Questions

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.

  1. The program will only print 5.

  2. The program will only print 1 and 4, in that order.

  3. The program will only print 1, 2, and 4, in that order.

  4. The program will only print 1, 4, and 5, in ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.