Java Exceptions  «Prev  Next»

Finally Keyword in Java - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 
1. Examine the following snippet of code. What do you expect the output to be?
try {
   Integer myInteger = new Integer("$");
} catch (Exception x) {
   System.out.println("error");
   return;
} finally {
   System.out.println("clean up");
}
-
Please select the best answer.
  A. error
clean up
  B. error
  C. No error at all!

2. Code within a finally block is
Please select the best answer.
  A. Always executed
  B. Executed unless there is a return statement in the try or catch block preceding it
  C. Depends on the version of the Java virtual machine.

3. What is wrong with the following code? Assume that the class MyFileObject is a valid class that is capable of representing a file in the file system, whose constructor takes a String indicating the file name and does not throw an exception, and that defines the methods open() and close(), and that open() might throw an IOException.
MyFileObject obj = new MyFileObject("test.in");
try {
   obj.open();
} catch(IOException) {
   System.out.println("an error occurred");
}

finally {
   obj.close();
}

Please select the best answer.
  A. The try clause is not defined correctly
  B. The catch clause is not defined correctly
  C. The finally clause is not defined correctly

Your score is 0.0
A
A
B