Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Beginning Java with NetBeans: Chapter 16 How to work with exceptions MULTIPLE CH

ID: 3728969 • Letter: B

Question

Beginning Java with NetBeans: Chapter 16

How to work with exceptions

MULTIPLE CHOICE [Answers are in tables – delete all but the correct answer’s 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 doesn’t 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 doesn’t 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.

Doesn’t 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 isn’t coded for it

b.

When the exception handler doesn’t contain any code

c.

When it isn’t caught or thrown

d.

When it’s 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 that’s 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 ____________________.

a.

Exception

c.

RuntimeException

b.

Error

d.

Throwable

Explanation / Answer

I have the answers for your questions.

1. Unchecked exceptions are the only exceptions derived from which class?

a.

Exception

c.

RuntimeException

b.

Error

d.

Throwable

Answer: Exceptions which are not checked at compile time are called Unchecked exceptions. Here, Error and RuntimeException are unchecked exceptions.

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

Answer: Any of these exceptions can occur in a Java application, so the answer is All the above.

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

Answer: One for each try block. But you can have as many as needed in a java program, if there are multiple try- catch blocks (one for each block)

4. The try-with-resources statement doesn’t 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

Answer: a try with resources automatically closes all declared resources at the end of the block, therefore, no need for a finally clause.

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.

Answer: Option b, a message that says “Too big. Try again:” is printed to the console and execution jumps to the top of the loop, as there is no NumberFormatException is thrown because 11 is a valid integer, this only satisfies the condition num>=10.

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.

Answer: Option d. A NumberFormatException is not thrown, the entry is stored in a variable, and execution jumps out of the loop. (as it violates the condition num>=10, breaks 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.

Answer: Option a. A NumberFormatException is thrown, execution jumps to the catch block, and a message that says “Invalid number” is printed to the console. (It throws a NumberFormatException when try to parse abc as an Integer), and then loop will execute again

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.

Answer: Option a. A ClassNotFoundException was not thrown. No two Exceptions will be thrown simultaneously, even if we use pipe (|) operator in catch block, any one of the Exceptions will be thrown and caught at once.

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 doesn’t matter

Answer: Most specific exception to least specific. Or else, if any parent- child Exception classes are there, the parent might catch the Exception meant for the child class.

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

Answer: multiple catch blocks to handle 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.

Doesn’t have to throw or handle any exceptions

Answer: option b. Throw or handle an IOException (as only IOException is thrown from the last catch block.)

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

Answer: save the details of an exception in a custom exception

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

Answer: Option c. It should be handled by the main method, as the method() may throw an IOException from methodB().

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(){...}

Answer: option c, public void getStudent() throws 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”

Answer: option c. “FileNotFoundException” and “Finally”

16. When is an exception swallowed?

a.

When an exception handler isn’t coded for it

b.

When the exception handler doesn’t contain any code

c.

When it isn’t caught or thrown

d.

When it’s caught by an exception handler for a more general exception

Answer: option d, When it’s caught by an exception handler for a more general exception

COMPLETION

17. All exception classes are derived from the Exception class.

18. Exceptions that you must handle before you can compile your code are called checked exceptions.

19. Code that’s executed in response to an exception is called a/an catch block.

20. Java locates the code that handles an exception by searching through the call stack.

21. To keep from losing debugging information when you throw a custom exception, you should use a feature called exception chaining or wrapping.

a.

Exception

c.

RuntimeException

b.

Error

d.

Throwable

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote