Introduction to Java Programming, Seventh Edition, Y. Daniel Liang

Chapter 18 Exceptions and Assertions


Section 18.3 Exceptions and Exception Types
1  A Java exception is an instance of __________.

A. RuntimeException
B. Exception
C. Error
D. Throwable.
E. NumberFormatException

2  An instance of _________ describes system errors. If this type of error occurs, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

A. RuntimeException
B. Exception
C. Error
D. Throwable.
E. NumberFormatException

3  An instance of _________ describes the errors caused by your program and external circumstances. These errors can be caught and handled by your program.

A. RuntimeException
B. Exception
C. Error
D. Throwable.
E. NumberFormatException

4  An instance of _________ describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors..

A. RuntimeException
B. Exception
C. Error
D. Throwable.
E. NumberFormatException

5  The following code causes Java to throw _________.
int number = Integer.MAX_VALUE + 1;

A. RuntimeException
B. Exception
C. Error
D. Throwable.
E. no exceptions

6  An instance of _________ are unchecked exceptions.

A. RuntimeException
B. Exception
C. Error
D. Throwable.
E. NumberFormatException

7  What exception type does the following program throw?
public class Test {
   public static void main(String[] args) {
     System.out.println(1 / 0);
   }
}

A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. ClassCastException
E. No exception

8  What exception type does the following program throw?
public class Test {
   public static void main(String[] args) {
     int[] list = new int[5];
     System.out.println(list[5]);
   }
}

A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. ClassCastException
E. No exception

9  What exception type does the following program throw?
public class Test {
   public static void main(String[] args) {
     String s = "abc";
     System.out.println(s.charAt(3));
   }
}

A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. ClassCastException
E. No exception

10  What exception type does the following program throw?
public class Test {
   public static void main(String[] args) {
     Object o = new Object();
     String d = (String)o;
   }
}

A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. ClassCastException
E. No exception

11  What exception type does the following program throw?
public class Test {
   public static void main(String[] args) {
     Object o = null;
     System.out.println(o.toString());
   }
}

A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. ClassCastException
E. NullPointerException

12  What exception type does the following program throw?
public class Test {
   public static void main(String[] args) {
     Object o = null;
     System.out.println(o);
   }
}

A. ArithmeticException
B. ArrayIndexOutOfBoundsException
C. StringIndexOutOfBoundsException
D. No exception
E. NullPointerException

Section 18.4 Understanding Exception Handling
13  A method must declare to throw ________.

A. unchecked exceptions
B. checked exceptions
C. Error
D. RuntimeException

14  Which of the following statements are true?

A. You use the keyword throws to declare exceptions in the method heading.
B. A method may declare to throw multiple exceptions.
C. To throw an exception, use the key word throw.
D. If a checked exception occurs in a method, it must be either caught or declared to be thrown from the method.

15  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.

16  Analyze the following code:

class Test {
   public static void main(String[] args) {
     try {
       String s = "5.6";
       Integer.parseInt(s); // Cause a NumberFormatException

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

A. The program displays NumberFormatException.
B. The program displays RuntimeException.
C. The program displays NumberFormatException followed by RuntimeException.
D. The program has a compilation error.

17  Analyze the following program.

class Test {
   public static void main(String[] args) {
     try {
       String s = "5.6";
       Integer.parseInt(s); // Cause a NumberFormatException

       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 Integer.parseInt(s);
B. An exception is raised due to 2 / i;
C. The program has a compilation error.
D. The program compiles and runs without exceptions.

18  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 (NumberFormatException ex) {
       System.out.println("NumberFormatException");
     }
     catch (RuntimeException ex) {
       System.out.println("RuntimeException");
     }
   }

   static void method() {
     String s = "5.6";
     Integer.parseInt(s); // Cause a NumberFormatException

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

A. The program displays NumberFormatException.
B. The program displays NumberFormatException followed by After the method call.
C. The program displays NumberFormatException followed by RuntimeException.
D. The program has a compilation error.
E. The program displays RuntimeException.

19  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("RuntimeException");
     }
     catch (Exception ex) {
       System.out.println("Exception");
     }
   }

   static void method() throws Exception {
     try {
       String s = "5.6";
       Integer.parseInt(s); // Cause a NumberFormatException

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

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

Section 18.5 The finally Clause
20  What is wrong in the following program?

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

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.

21  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

22  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

23  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.

24  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.

25  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.

26  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.

27  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, and then terminates because of an unhandled exception.

Section 18.6 When to Use Exceptions
28  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.

29  Analyze the following code:

class Test {
   public static void main(String[] args) {
     try {
       int zero = 0;
       int y = 2/zero;
       try {
         String s = "5.6";
         Integer.parseInt(s); // Cause a NumberFormatException
       }
       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.

Section 18.7 Rethrowing Exceptions
30  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("RuntimeException");
     }
     catch (Exception ex) {
       System.out.println("Exception");
     }
   }

   static void method() throws Exception {
     try {
       String s = "5.6";
       Integer.parseInt(s); // Cause a NumberFormatException

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

A. The program displays NumberFormatException twice.
B. The program displays NumberFormatException followed by After the method call.
C. The program displays NumberFormatException followed by RuntimeException.
D. The program has a compilation error.

Section 18.8 (Optional) Chained Exceptions
31  Which of the following statement is correct to rethrow exception ex along with new information?


A. throw new Exception("New info", ex);
B. throw ex; throw new Exception("Some new info");
C. throw new Exception(ex, "New info");
D. throw new Exception("New info"); throw ex;

Section 18.10 (Optional) Assertions
32  Which of the following is not a correct assertion statement?

A. assert (i > 10);
B. assert sum > 10 && sum < 5 * 10 : "sum is " + sum;
C. assert "sum is " + sum;
D. None of the above

33  Which of the following is true?
A. Exception handling deals with unusual circumstances during program execution. Assertions are intended to ensure the correctness of the program.
B. Exception handling addresses robustness whereas assertion addresses correctness.
C. Do not use assertions for argument checking in public methods.
D. Use assertions to reaffirm assumptions.
E. All of the above.