Exceptions Self Test With Answers                       // 2 marks each


1) Select the incorrect statement.

a) Checked exceptions arise in a correct program.
b) In a correct program, runtime exceptions  should never occur.
c) You are not expected to handle Error class objects.
d) Throwable are a subclass of the Checked Exception category         ( d )

2) The following code will

  class Dv{
               public static void main(String[] args) {
                 System.out.println("Running Test");
                 int x =1 / 0;
                }
         }

a) not compile
b) compile but fail to run
c) compile and run and throw a runtime exception
d) compile and run successfully without error reports                            ( c )

3)  The sleep method throws InterruptedException. What will the following code do.

 class X2{
     public static void main(String args[]) throws InterruptedException {
         Thread.sleep(100);
         }
     }

a) does not compile
b) compiles but fails to run
c) compiles and runs and throws a runtime exception
d) compiles and runs successfully but may throw an exception           ( d )

//   Comment: this is a bit of a loaded question. Though sleep may throw an
//   InterruptedException it normally won't and so usually it will compile and
//   run successfully so d is the answer.
 

5) Which of the following statements regarding  exceptions is not correct?

a) there can only be one finally block
b) for every try block there must be either a catch or finally block
c) for every try and catch block there must also be a finally block
d) there can be any number of catch blocks                                        ( c )