Question: Beginning Java with NetBeans: Chapter 16 How to work with exceptions MULTIPLE CHOICE [Answers are in tables delete all but the correct answers cell] 1.
Beginning Java with NetBeans: Chapter 16
How to work with exceptions
MULTIPLE CHOICE [Answers are in tables delete all but the correct answers cell]
1. Unchecked exceptions are the only exceptions derived from which class?
| a. | Exception | c. | RuntimeException |
| b. | Error | d. | Throwable |
2. Which of the following classes define exceptions that can occur in a Java application?
| a. | ArithmeticException | d. | all of the above |
| b. | NumberFormatException | e. | none of the above |
| c. | NullPointerException | ||
3. What is the maximum number of finally blocks that you can code?
| a. | One for each exception | c. | As many as needed |
| b. | One for each try block | d. | 10 |
4. The try-with-resources statement doesnt need a finally clause because it
| a. | uses a multi-catch block to catch all exceptions |
| b. | implements the AutoCloseable interface |
| c. | automatically closes all declared resources |
| d. | catches all exception that are thrown when you try to close a resource |
Code example 16-1
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number less than 10: ");
int num;
while (true) {
try {
num = Integer.parseInt(sc.nextLine());
if (num >= 10) {
System.out.print("Too big. Try again: ");
continue;
}
break;
} catch(NumberFormatException e) {
System.out.print("Invalid number. Try again: ");
}
}
5. (Refer to code example 16-1.) What happens if the user enters 11 at the prompt?
| a. | A NumberFormatException is thrown, execution jumps to the catch block, and a message that says Invalid number. Try again: is printed to the console. |
| b. | A message that says Too big. Try again: is printed to the console and execution jumps to the top of the loop. |
| c. | A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps to the top of the loop. |
| d. | A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps out of the loop. |
6. (Refer to code example 16-1.) What happens if the user enters 5 at the prompt?
| a. | A NumberFormatException is thrown, execution jumps to the catch block, and a message that says Invalid number is printed to the console. |
| b. | A message that says Too big is printed to the console and execution jumps to the top of the loop. |
| c. | A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps to the top of the loop. |
| d. | A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps out of the loop. |
7. (Refer to code example 16-1.) What happens if the user enters abc at the prompt?
| a. | A NumberFormatException is thrown, execution jumps to the catch block, and a message that says Invalid number is printed to the console. |
| b. | A message that says Too big is printed to the console and execution jumps to the top of the loop. |
| c. | A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps to the top of the loop. |
| d. | A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps out of the loop. |
Code example 16-2
public static void connectAndOpenDB() {
try {
StudentDB.connect(); //can throw a ClassNotFoundException
StudentDB.open(); //can throw an SQLException
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e.getMessage());
}
System.out.println("Connected and opened.");
}
8. (Refer to code example 16-2.) Which of the following is true if an SQLException is thrown?
| a. | A ClassNotFoundException was not thrown. |
| b. | A ClassNotFoundException was also thrown. |
| c. | Neither of the statements that use the println method were called. |
| d. | The println method after the catch clause was not called. |
9. When coding catch blocks, you should always code them in what order?
| a. | Most specific exception to least specific |
| b. | Least specific exception to most specific |
| c. | Most important exception to least important |
| d. | Order doesnt matter |
10. A multi-catch block allows you to use
| a. | multiple catch blocks to handle multiple exceptions that are at the same level in the exception hierarchy |
| b. | multiple catch blocks to handle exceptions that are at different levels in the exception hierarchy |
| c. | a single catch block to handle multiple exceptions that are at the same level in the exception hierarchy |
| d. | a single catch block to handle multiple exceptions that are at different levels in the exception hierarchy |
11. If the following code is located in a method named readData, what must a method that calls readData do?
try (RandomAccessFile in = new RandomAccessFile("f.dat", "r")) {
long length = in.length();
} catch(FileNotFoundException fnfe) {
System.err.println("file.dat doesn't exist");
} catch(IOException ioe) {
System.err.println("error in length method of RAF");
throw ioe;
}
| a. | Throw or handle a FileNotFoundException |
| b. | Throw or handle an IOException |
| c. | Throw or handle a FileNotFoundException and an IOException |
| d. | Doesnt have to throw or handle any exceptions |
12. Exception chaining lets you
| a. | handle more than one exception in the same method |
| b. | save the details of an exception in a custom exception |
| c. | create a list of methods in the reverse order in which they were called |
| d. | keep a log of the exceptions that occur as an application executes |
13. In the code that follows, which method handles the IOException?
public static void main(String[] args) {
methodA();
}
private static void methodA() throws IOException {
methodB();
}
private static void methodB() throws IOException {
throw new IOException();
}
| a. | methodA | c. | main |
| b. | methodB | d. | none |
14. To make the code that follows compile, what should the declaration of the getStudent method be?
public void getStudent() {
readGrades();
}
public void readGrades() throws IOException {
boolean gradesAvailable = true;
if (gradesAvailable == false) {
throw new IOException();
}
}
| a. | public void getStudent(){...} |
| b. | public static void getStudent(){...} |
| c. | public void getStudent() throws IOException{...} |
| d. | public void getStudent() throw new IOException(){...} |
15. What is printed to the console if a FileNotFoundException occurs in the try block that follows?
try {
file = new RandomAccessFile(fileName, "r");
validName = true;
} catch(FileNotFoundException e) {
System.out.println("FileNotFoundException.");
} catch(IOException e) {
System.out.println("IOException");
} finally {
System.out.println("Finally");
}
| a. | FileNotFoundException |
| b. | FileNotFoundException and IOException |
| c. | FileNotFoundException and Finally |
| d. | Finally |
16. When is an exception swallowed?
| a. | When an exception handler isnt coded for it |
| b. | When the exception handler doesnt contain any code |
| c. | When it isnt caught or thrown |
| d. | When its caught by an exception handler for a more general exception |
COMPLETION
17. All exception classes are derived from the ____________ class.
18. Exceptions that you must handle before you can compile your code are called ____________ exceptions.
19. Code thats executed in response to an exception is called a/an ____________________.
20. Java locates the code that handles an exception by searching through the ________________.
21. To keep from losing debugging information when you throw a custom exception, you should use a feature called exception ____________________.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
