Quiz on Exceptions

This is a practice quiz. The results are not recorded anywhere and do not affect your grade. The questions on this quiz might not appear in any quiz or test that does count toward your grade.

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.


1. What method of an Exception object prints a list of methods that were called before the exception was thrown?

a.    printErrors()
b.    getMessage()
c.    printStackTrace()
d.    traceStack()

Correct Answer Is:


2. What method of an Exception object returns a message string?

a.    getError()
b.    getMessage()
c.    printMessage()
d.    traceMessage()

Correct Answer Is:


3. What happens during execution if an negative value is used for an array index?

a.    An IndexOutOfBoundsException is thrown.
b.    A NumberFormatException is thrown.
c.    The first slot of the array is used.
d.    This is an Error, so the program immediately terminates no matter what.

Correct Answer Is:


4. A method must do either of two things with a checked exception. What are those two things?

a.    (1) Ignore the exception, or (2) check the exception.
b.    (1) Handle the exception in a catch{} block, or (2) return the exception to the sender.
c.    (1) Handle the exception in a catch{} block, or (2) throw the exception to the method that called this method.
d.    (1) Handle the exception in a catch{} block, or (2) handle the exception in a try{} block,

Correct Answer Is:


5. What is the only type of exception that is NOT checked?

a.    Class Exception.
b.    Class RunTimeException and its subclasses.
c.    Class IOException and its subclasses.
d.    Class ArithmeticException only.

Correct Answer Is:


6. Say that methodA calls methodB, and methodB calls methodC. MethodC might throw a NumberFormatException. Can the program be written so that methodA handles the exception?

a.    No, the exception must be handled by methodC or its caller, methodB.
b.    No, methodC must handle the exception.
c.    Yes, if the header for methodC says ...throws NumberFormatException
d.    Yes, if the headers for methodC and methodB say ...throws NumberFormatException

Correct Answer Is:


7. Which of the following is a correct outline of a method that throws an exception when it finds a problem?

a.   
public void someMethod ()
{
  ...
  if ( problem )
    throw new Exception("Useful Message");
  ...
}
b.   
public void someMethod () throws Exception
{
  ...
  if ( problem )
    Exception("Useful Message");
  ...
}
c.   
public void someMethod () throws Exception
{
  ...
  if ( problem )
    throw( new Exception("Useful Message") );
  ...
}
d.   
public void someMethod () throws Exception
{
  ...
  if ( problem )
    throw new Exception("Useful Message");
  ...
}

Correct Answer Is:


8. MethodX might encounter an IOException or an AWTException, but handles neither. How should the header for methodX be written?

a.   
... methodX(...) throws IOException, AWTException
b.   
... methodX(...) throws IOException or AWTException
c.   
... methodX(...) throws IOException throws AWTException
d.   
... methodX(...) throws (IOException, AWTException)

Correct Answer Is:


9. You are writing a program to check people into a hotel. People over 65 get a 10% discount. How would you write the program?

a.    Use an if statement to test for age 65 or greater. If one is detected, throw an exception. A catch{} block will apply the discount.
b.    Normal if-else programming logic will be used.
c.    Subtract 65 from the person's age and divide the bill by the result. If an ArithmeticException exception is thrown, apply the 10% discount in the exception handler.
d.    Create an array with 120 elements, one element for each possible hotel guest age. Each element contains the discount rate for that age.

Correct Answer Is:


10. Say that a method catches an IOException in a catch{} block. Is it possible for that block to do some processing and then throw the same exception to the caller?

a.    No—when an exception is caught the Exception object is destroyed.
b.    No—an exception may be caught only once.
c.    Yes—as long as the method also has a throws clause for that exception.
d.    Yes—but the catch{} block must construct a new Exception object.

Correct Answer Is:


The number you got right:       Percent Correct:       Letter Grade:   

Click here (If you have just returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the "shift key" while clicking on reload to clear the old answers.)