Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 18:39:07 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 13: Exception Handling

In the Questions 1-16, assume that the divide method in the Rational class in Example 9.2 is modified as follows:
   public Rational divide(Rational secondRational) throws RuntimeException {
     if (secondRational.getNumerator() == 0)
       throw new RuntimeException("Divisor cannot be zero");

     long n = numerator * secondRational.getDenominator();
     long d = denominator * secondRational.getNumerator();
     return new Rational(n, d);
   }

The divide method in the Rational class throws RuntimeException if the divisor is 0.


Sections 13.2-13.4
1   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       Rational r1 = new Rational(3, 4);
       Rational r2 = new Rational(0, 1);
       Rational x = r1.divide(r2);

       int i = 0;
       int y = 2 / i;
     }
     catch (Exception ex) {
       System.out.println("Rational operation error ");
     }
     catch (RuntimeException ex) {
       System.out.println("Integer operation error");
     }
   }
}

A. The program displays Rational operation error.
B. The program displays Integer operation error.
C. The program displays Rational operation error followed by Integer operation error.
D. The program has a compilation error.

2   Analyze the following program.

class Test {
   public static void main(String[] args) {
     try {
       Rational r1 = new Rational(3, 4);
       Rational r2 = new Rational(0, 1);
       Rational x = r1.divide(r2);

       int i = 0;
       int y = 2 / i;
       System.out.println("Welcome to Java");
     }
     catch (Exception ex) {
       System.out.println(ex);
     }
   }
}

A. An exception is raised due to r1.divide(r2);
B. An exception is raised due to 2 / i;
C. Both a and b.
D. The program has a compilation error.
E. The program compiles and runs without exceptions.

3   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       method();
       System.out.println("After the method call");
     }
     catch (RuntimeException ex) {
       System.out.println("Integer operation error");
     }
     catch (Exception ex) {
       System.out.println("Rational operation error");
     }
   }

   static void method() throws Exception {
     Rational r1 = new Rational(3, 4);
     Rational r2 = new Rational(0, 1);
     Rational x = r1.divide(r2);

     int i = 0;
     int y = 2 / i;
     System.out.println("Welcome to Java");
   }
}

A. The program displays Rational operation error.
B. The program displays Rational operation error followed by After the method call.
C. The program displays Rational operation error followed by Integer operation error.
D. The program has a compilation error.
E. The program displays Integer operation error.

4   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       method();
       System.out.println("After the method call");
     }
     catch (RuntimeException ex) {
       System.out.println("Integer operation error");
     }
     catch (Exception ex) {
       System.out.println("Rational operation error");
     }
   }

   static void method() throws Exception {
     try {
       Rational r1 = new Rational(3, 4);
       Rational r2 = new Rational(0, 1);
       Rational x = r1.divide(r2);

       int i = 0;
       int y = 2 / i;
       System.out.println("Welcome to Java");
     }
     catch (RuntimeException ex) {
       System.out.println("Integer operation error");
     }
     catch (Exception ex) {
       System.out.println("Rational operation error");
     }
   }
}

A. The program displays Rational operation error twice.
B. The program displays Integer operation error twice.
C. The program displays Integer operation error followed by After the method call.
D. The program displays Rational operation error followed by Integer operation error.
E. The program has a compilation error.

5   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       method();
       System.out.println("After the method call");
     }
     catch (RuntimeException ex) {
       System.out.println("Integer operation error");
     }
     catch (Exception ex) {
       System.out.println("Rational operation error");
     }
   }

   static void method() throws Exception {
     try {
       Rational r1 = new Rational(3, 4);
       Rational r2 = new Rational(0, 1);
       Rational x = r1.divide(r2);

       int i = 0;
       int y = 2 / i;
       System.out.println("Welcome to Java");
     }
     catch (RuntimeException ex) {
       System.out.println("Integer operation error");
     }
     catch (Exception ex) {
       System.out.println("Rational operation error");
       throw ex;
     }
   }
}

A. The program displays Integer operation error twice.
B. The program displays Integer operation error followed by After the method call.
C. The program displays Rational operation error followed by Integer operation error.
D. The program has a compilation error.

6   Analyze the following code:

class Test {
   public static void main(String[] args)
     throws MyException {
     System.out.println("Welcome to Java");
   }
}

class MyException extends Error {
}

A. You should not declare a class that extends Error, because Error raises a fatal error that terminates the program.
B. You cannot declare an exception in the main method.
C. You declared an exception in the main method, but you did not throw it.
D. The program has a compilation error.

Section 13.5 The finally Clause
7   What is wrong in the following program?

class TestRationalWithException {
   public static void main (String[] args) {
     Rational r1 = new Rational(4, 2);
     Rational r2 = new Rational(2, 3);
     Rational r3 = new Rational(0, 1);
   
     try {
       System.out.println(
         r1 + " + " + r2 + " = " + r1.add(r2));
       System.out.println(
         r1 + " - " + r2 + " = " + r1.subtract(r2));
       System.out.println(
         r1 + " * " + r2 + " = " + r1.multiply(r2));
       System.out.println(
         r1 + " + " + r2 + " = " + r1.add(r2));
     }
   }
}

A. You cannot have a try block without a catch block.
B. You cannot have a try block without a catch block or a finally block.
C. A method call that does not declare exceptions cannot be placed inside a try block.
D. Nothing is wrong.

8   What is displayed on the console when running the following program?

class Test {
   public static void main (String[] args) {
     try {
       System.out.println("Welcome to Java");
     }
     finally {
       System.out.println("The finally clause is executed");
     }
   }
}

A. Welcome to Java
B. Welcome to Java followed by The finally clause is executed in the next line
C. The finally clause is executed
D. None of the above

9   What is displayed on the console when running the following program?

class Test {
   public static void main (String[] args) {
     try {
       System.out.println("Welcome to Java");
       return;
     }
     finally {
       System.out.println("The finally clause is executed");
     }
   }
}

A. Welcome to Java
B. Welcome to Java followed by The finally clause is executed in the next line
C. The finally clause is executed
D. None of the above

10   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       System.out.println("Welcome to Java");
       int i = 0;
       int y = 2/i;
       System.out.println("Welcome to HTML");
     }
     finally {
       System.out.println("The finally clause is executed");
     }
   }
}

A. Welcome to Java.
B. Welcome to Java followed by The finally clause is executed in the next line.
C. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
D. None of the above.

11   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       System.out.println("Welcome to Java");
       int i = 0;
       double y = 2.0 / i;
       System.out.println("Welcome to HTML");
     }
     finally {
       System.out.println("The finally clause is executed");
     }
   }
}

A. Welcome to Java.
B. Welcome to Java followed by The finally clause is executed in the next line.
C. The program displays three lines: Welcome to Java, Welcome to HTML, The finally clause is executed.
D. None of the above.

12   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       System.out.println("Welcome to Java");
       int i = 0;
       int y = 2/i;
       System.out.println("Welcome to Java");
     }
     catch (RuntimeException ex) {
       System.out.println("Welcome to Java");
     }
     finally {
       System.out.println("End of the block");
     }
   }
}

A. The program displays Welcome to Java three times followed by End of the block.
B. The program displays Welcome to Java two times followed by End of the block.
C. The program displays Welcome to Java three times.
D. The program displays Welcome to Java two times.

13   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       System.out.println("Welcome to Java");
       int i = 0;
       int y = 2/i;
       System.out.println("Welcome to Java");
     }
     catch (RuntimeException ex) {
       System.out.println("Welcome to Java");
     }
     finally {
       System.out.println("End of the block");
     }
   
     System.out.println("End of the block");
   }
}

A. The program displays Welcome to Java three times followed by End of the block.
B. The program displays Welcome to Java two times followed by End of the block.
C. The program displays Welcome to Java two times followed by End of the block two times.
D. You cannot catch RuntimeException errors.

14   What is displayed on the console when running the following program?

class Test {
   public static void main(String[] args) {
     try {
       System.out.println("Welcome to Java");
       int i = 0;
       int y = 2/i;
       System.out.println("Welcome to Java");
     }
     finally {
       System.out.println("End of the block");
     }
   
     System.out.println("End of the block");
   }
}

A. The program displays Welcome to Java three times followed by End of the block.
B. The program displays Welcome to Java two times followed by End of the block.
C. The program displays Welcome to Java two times followed by End of the block two times.
D. The program displays Welcome to Java and End of the block, then terminates because of an unhandled exception.

Section 13.6 When to Use Exceptions
15   Which of the following is not an advantage of Java exception handling?

A. Java separates exception handling from normal processing tasks.
B. Exception handling improves performance.
C. Exception handling makes it possible for the caller's caller to handle the exception.
D. Exception handling simplifies programming because the error-reporting and error-handling code can be placed at the catch block.

16   Analyze the following code:

class Test {
   public static void main(String[] args) {
     try {
       int zero = 0;
       int y = 2/zero;
       try {
         Rational r1 = new Rational(2, 3);
         Rational r2 = new Rational(0, 1);
         Rational r3 = r1.divide(r2);
       }
       catch(Exception e) {
       }
     }
     catch(RuntimeException e) {
       System.out.println(e);
     }
   }
}
A. A try-catch block cannot be embedded inside another try-catch block.
B. A good programming practice is to avoid nesting try-catch blocks, because nesting makes programs difficult to read. You can rewrite the program using only one try-catch block.
C. The program has a compilation error because Exception appears before RuntimeException.
D. None of the above.