Review Questions

graphics/rq_icon.gif

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 that order.

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

  6. The program will only print 3 and 5, in that order.

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.

  1. If run with no arguments, the program will produce no output.

  2. If run with no arguments, the program will print "The end".

  3. The program will throw an ArrayIndexOutOfBoundsException.

  4. If run with one argument, the program will simply print the given argument.

  5. If run with one argument, the program will print the given argument followed by "The end".

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.

  1. The code will fail to compile, since the main() method does not declare that it throws RuntimeException in its declaration.

  2. The program will fail to compile, since it cannot throw re.

  3. The program will compile without error and will throw java.lang.RuntimeException when run.

  4. The program will compile without error and will throw java.lang.NullpointerException when run.

  5. The program will compile without error and will run and terminate without any output.

5.18

Which statements are true?

Select the two correct answers.

  1. If an exception is uncaught in a method, the method will terminate and normal execution will resume.

  2. An overriding method must declare that it throws the same exception classes as the method it overrides.

  3. The main() method of a program can declare that it throws checked exceptions.

  4. A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class.

  5. finally blocks are executed if, and only if, an exception gets thrown while inside the corresponding try block.

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.

  1. The program will print 5.

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

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

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

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

  6. The program will print 3 and 5, in that order.

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.

  1. The program will print 2 and throw InterruptedException.

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

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

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

  5. The program will print 3 and 2, in that order.

  6. The program will print 1 and 3, in that order.

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.

  1. The main() method must declare that it throws B.

  2. The finally block must follow the catch block in the main() method.

  3. The catch block in the main() method must declare that it catches B rather than A.

  4. A single try block cannot be followed by both a finally and a catch block.

  5. The declaration of class A is illegal.

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.

  1. Does not need to specify any exceptions.

  2. Needs to specify that it throws ArithmeticException.

  3. Needs to specify that it throws InterruptedException.

  4. Needs to specify that it throws RuntimeException.

  5. Needs to specify that it throws both ArithmeticException and InterruptedException.

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.

  1. The main() method must declare that it throws RuntimeException.

  2. The overriding f() method in MyClass must declare that it throws ArithmeticException, since the f() method in class A declares that it does.

  3. The overriding f() method in MyClass is not allowed to throw InterruptedException, since the f() method in class A does not throw this exception.

  4. The compiler will complain that the catch(ArithmeticException) block shadows the catch(Exception) block.

  5. You cannot throw exceptions from a catch block.

  6. Nothing is wrong with the code, it will compile without errors.



     
    ASPTreeView.com
     
    Evaluation has КїЧЅГexpired.
    Info...