revised: 10/05/03, 11/07/2012, 10/08/2017


Quiz on Exceptions

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:


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

A.    getError()

B.    getMessage()

C.    printMessage()

D.    traceMessage()

Correct Answer:


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:


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:


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:


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:


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 )
    new Exception("Useful Message");
  ...
}

B.   

public void someMethod() throws Exception
{
  ...
  if ( problem )
    Exception("Useful Message");
  ...
}

C.   

public void someMethod() throws Exception
{
  ...
  if ( problem )
    try( new Exception("Useful Message") );
  ...
}

D.   

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

Correct Answer:


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:


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:


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:


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


Click here If you have 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 to clear the old answers.