Verified Test Bank Exceptions and Advanced File I/O Ch11 - Java Control Structures 7e Test Bank by Tony Gaddis. DOCX document preview.

Verified Test Bank Exceptions and Advanced File I/O Ch11

Starting Out with Java: From Control Structures through Objects 7e (Gaddis)

Chapter 11 Exceptions and Advanced File I/O

TRUE/FALSE

1. The call stack is an internal list of all the methods that are currently executing.

2. When an object is serialized, it is converted into a series of bytes that contain the object's data.

3. A class must implement the Serializable interface in order for the objects of the class to be serialized.

4. If the class SerializedClass contains references to objects of other classes as fields, those classes must also implement the Serializable interface in order to be serialized.

5. When catching multiple exceptions that are related to one another through inheritance you should handle the more general exception classes before the more specialized exception classes.

6. When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.

7. The throws clause causes an exception to be thrown.

8. When you deserialize an object using the readObject method, you must cast the return value to the desired class type.

9. In a try statement, the try clause must appear first, followed by all of the catch clauses, followed by the optional finally clause.

10. A catch clause that uses a parameter variable of the Exception type is capable of catching any exception that extends the Error class.

11. When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown.

12. The throw statement informs the compiler that a method throws one or more exceptions.

13. The ability to catch multiple types of exceptions with a single catch clause is known as multi-catch and was introduced in Java 7.

14. In versions of Java prior to Java 7 each catch clause could handle only one type of exception.

MULTIPLE CHOICE

1. Beginning with Java7, to catch multiple exceptions with a single catch, you can use __________.

a.

multi-catch

c.

multi-try

b.

catch templates

d.

catchAll

2. When you write a method that throws a checked exception, you must __________.

a.

override the default error method

b.

ensure that the error will occur at least once each time the program is executed

c.

have a throws clause in the method header

d.

use each class only once in a method

3. When writing a string to a binary file or reading a string from a binary file, it is recommended that you use __________.

a.

methods that use UTF-8 encoding

b.

the FileReader and Writer class methods

c.

the Scanner class methods

d.

the System.In and System.out methods

4. All of the exceptions you will handle are instances of classes that extend the __________ class.

a.

RunTimeException

c.

Error

b.

IOException

d.

Exception

5. All exceptions are instances of classes that extend the __________ class.

a.

RunTimeException

c.

Error

b.

Throwable

d.

Exception

6. The catch clause __________.

a.

starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable

b.

follows the try clause

c.

contains code to gracefully handle the exception type listed in the parameter list

d.

The catch clause does all of these

7. What happens if a program does not handle an unchecked exception?

a.

This isn't possible; the program must handle the exception.

b.

The program is halted and the default exception handler handles the exception.

c.

The exception is ignored.

d.

A compiler error will occur.

8. If your code does not handle an exception when it is thrown, __________ prints an error message and crashes the program.

a.

the Java error handler.

c.

default exception handler

b.

multi-catch

d.

try statement

9. A(n) __________ is an object that is generated in memory as the result of an error or an unexpected event.

a.

exception

c.

default exception handler

b.

exception handler

d.

error message

10. A(n) __________ contains one or more statements that are executed and can potentially throw an exception.

a.

exception block

c.

try block

b.

catch block

d.

error block

11. When an exception is thrown __________.

a.

it must always be handled by the method that throws it

b.

it must be handled by the program or by the default exception handler

c.

it may be ignored

d.

the program terminates even if the exception is handled

12. In a try/catch construct, after the catch statement is executed __________.

a.

the program returns to the statement following the statement in which the exception occurred

b.

the program terminates

c.

the program resumes at the statement that immediately follows the try/catch construct

d.

the program resumes at the first statement of the try statement

13. A(n) __________ is a section of code that gracefully responds to exceptions when they are thrown.

a.

exception

c.

exception handler

b.

default exception handler

d.

thrown class

14. In Java there are two categories of exceptions which are __________.

a.

unchecked and checked

c.

critical and nominal

b.

runtime and compile-time

d.

static and dynamic

15. A file that contains raw binary data is known as a __________.

a.

binary file

c.

serial file

b.

machine file

d.

raw data file

16. In order for an object to be serialized, its class must implement the __________ interface.

a.

Serial

c.

ObjectOutputStream

b.

Serializable

d.

Writeable

17. An exception's default error message can be retrieved by using the __________ method.

a.

getMessage()

c.

getDefaultMessage()

b.

getErrorMessage()

d.

getDefaultErrorMessage()

18. An exception object's default error message can be retrieved using the __________ method.

a.

getMessage

c.

getDefaultErrorMessage

b.

getDefaultMessage

d.

getErrorMessage

19. All of the exceptions that you will handle are instances of classes that extend the __________ class.

a.

IOException

c.

Error

b.

Exception

d.

RunTimeException

20. To write data to a binary file, you create objects from which of the following classes?

a.

File and Scanner

b.

BinaryFileWriter and BinaryDataWriter

c.

FileOutputStream and DataOutputStream

d.

File and PrintWriter

21. To read data from a binary file, you create objects from which of the following classes?

a.

File and Scanner

b.

File and PrintWriter

c.

BinaryFileReader and BinaryDataReader

d.

FileInputStream and DataInputStream

22. If a random access file contains a stream of characters, which of the following statements would move the file pointer to the starting byte of the fifth character in the file?

a.

file.seek(4);

c.

file.seek(10);

b.

file.seek(8);

d.

file.seek(5);

23. To serialize an object and write it to the file, use the __________ method of the ObjectOutputStream class.

a.

WriteObject

c.

Serialize

b.

SerializeObject

d.

SerializeAndWrite

24. The numeric classes' parse methods all throw an exception of __________ type if the string being converted does not contain a convertible numeric value.

a.

NumberFormatException

c.

ExceptionMessage

b.

ParseIntException

d.

FileNotFoundException

25. The try statement may have an optional __________ clause which must appear after all of the catch clauses.

a.

try-again

c.

default

b.

finally

d.

abort

26. In a multi-catch (introduced in Java 7) the exception types are separated in the catch clause by the __________ symbol.

a.

*

b.

?

c.

|

d.

&

27. When using the throw statement, if you don't pass a message to the exception object's constructor, then __________.

a.

the exception will have a null message

b.

the exception will have a message containing the address of the exception

c.

the exception will have a default message describing the exception

d.

a compiler error will occur

28. The IllegalArgumentException class extends the RuntimeException class and is, therefore, __________.

a.

a checked exception class

c.

never used directly

b.

an unchecked exception class

d.

None of these

29. If the code in a method can potentially throw a checked exception, then that method must __________.

a.

handle the exception

b.

have a throws clause listed in the method header

c.

neither of these

d.

either of these

30. If a method does not handle a possible checked exception, what must the method have?

a.

a try clause in its header

c.

a finally clause in its header

b.

a catch clause in its header

d.

a throws clause in its header

31. When an exception is thrown by code in its try block, the JVM begins searching the try statement for a catch clause that can handle it and passes control of the program to __________.

a.

the last catch clause that can handle the exception

b.

each catch clause that can handle the exception

c.

the first catch clause that can handle the exception

d.

the statement that appears immediately after the catch block

32. Unchecked exceptions are those that inherit from the __________.

a.

Error class or the RuntimeException class

b.

Error class or the Exception class

c.

Error class

d.

ExceptionClass class or the RuntimeException class

33. In Windows, which of the following statements will open the file, InputFile.txt, that is in the root directory on the C: drive?

a.

FileReader freader = new FileReader("C:\InputFile.txt");

b.

FileReader freader = new FileReader("C:\InputFile\txt");

c.

FileReader freader = new FileReader("/c/InputFile.txt");

d.

FileReader freader = new FileReader("C:\\InputFile.txt");

34. Classes that inherit from the Error class are for exceptions that are thrown when __________.

a.

an IOException occurs, and the application program should not try to handle them

b.

a critical error occurs, and the application program should not try to handle them

c.

an IOException occurs, and the application program should try to handle them

d.

a critical error occurs, and the application program should try to handle them

35. The RandomAccessFile class treats a file as a stream of __________.

a.

bytes

b.

characters

c.

integers

d.

data

36. What will be the result of the following statements?

FileInputStream fstream = new FileInputStream("Input.dat");

DataInputStream inFile = new DataInputStream(fstream);

a.

The inFile variable will reference an object that is able to read random access data from the Input.dat file.

b.

The inFile variable will reference an object that is able to read text data from the Input.dat file.

c.

The inFile variable will reference an object that is able to read binary data from the Input.dat file.

d.

The inFile variable will reference an object that is able to write binary data to the Input.dat file.

37. What will be the result of the following statements?

FileOutputStream fstream = new FileOutputStream("Output.dat");

DataOutputStream outputFile = new DataOutputStream(fstream);

a.

The outputFile variable will reference an object that is able to write text data only to the Output.dat file.

b.

The outputFile variable will reference an object that is able to write binary data to the Output.dat file.

c.

The outputFile variable will reference an object that is able to read binary data from the Output.dat file.

d.

The outputFile variable will reference an object that is able to write binary data to the Output.dat as a random access file.

38. In the following code, assume that inputFile references a Scanner object that has been successfully used to open a file:

double totalIncome = 0.0;

while (inputFile.hasNext())

{

try

{

totalIncome += inputFile.nextDouble();

}

catch(InputMismatchException e)

{

System.out.println("Non-numeric data encountered " +

"in the file.");

inputFile.nextLine();

}

finally

{

totalIncome = 35.5;

}

}

What will be the value of totalIncome after the following values are read from the file?

2.5

8.5

3.0

5.5

abc

1.0

a.

19.5.

b.

0.0

c.

35.5

d.

75.0

39. The following catch statement can __________.

catch (Exception e) {...}

a.

handle all throwable objects by using a polymorphic reference for the parameter

b.

handle all exceptions that are instances of the Exception class or one of its subclasses

c.

handle all exceptions that are instances of the Exception class but not its subclasses

d.

cause a compiler error

40. If the data.dat file does not exist, what will happen when the following statement is executed?

RandomAccessFile randomFile = new RandomAccessFile("data.dat", "rw");

a.

An IOException will be thrown.

b.

The file, data.dat, will be created.

c.

A FileNotFoundException will be thrown.

d.

A compiler error will occur.

41. Given the following constructor code, which of the statements are true?

public Book(String ISBNOfBook, double priceOfBook,

int numberOrderedOfBook)

{

if (ISBNOfBook == "")

throw new BlankISBN();

if (priceOfBook < 0)

throw new NegativePrice(priceOfBook);

if (numberedOrderedOfBook < 0)

throw new NegativeNumberOrdered(numberOrderedv);

ISBN = ISBNOfBook;

price = priceOfBook;

numberedOrdered = numberOrderedOfBook;

}

a.

There is an error: a throws clause should be added to the constructor header.

b.

Classes extending the Exception class should be created for each of the custom exceptions that are thrown in the constructor.

c.

A FileNotFoundException will be thrThe calling method must handle the exceptions thrown in the constructor, or have its own throws clause specifying them.

d.

All of these are true.

42. Assume that the classes BlankISBN, NegativePrice, and NegativeNumberOrdered are exception classes that inherit from Exception. The following code is a constructor for the Book class. What must be true about any method that instantiates the Book class with this constructor?

public Book(String ISBNOfBook, double priceOfBook,

int numberOrderedOfBook)

{

if (ISBNOfBook == "")

throw new BlankISBN();

if (priceOfBook < 0)

throw new NegativePrice(priceOfBook);

if (numberedOrderedOfBook < 0)

throw new NegativeNumberOrdered(numberOrderedv);

ISBN = ISBNOfBook;

price = priceOfBook;

numberedOrdered = numberOrderedOfBook;

}

a.

It must call the constructor with valid data or a compiler error will occur.

b.

It must contain an inner class that extends the IOException class.

c.

It must handle all of the possible exceptions thrown by the constructor or have its own throws clause specifying them.

d.

All of these are true.

43. In a catch statement, what does the following code do?

System.out.println(e.getMessage());

a.

It prints the code that caused the exception.

b.

It prints the stack trace.

c.

It prints the error message for an exception.

d.

It overrides the toString method.

44. If you want to append data to an existing binary file, BinaryFile.dat, which of the following statements would you use to open the file?

a.

FileOutputStream fstream =

new FileOutputStream("BinaryFile.dat");

DataOutputStream binaryOutputFile =

new DataOutputStream(fstream);

b.

FileOutputStream fstream =

new FileOutputStream("BinaryFile.dat", false);

DataOutputStream binaryOutputFile =

new DataOutputStream(fstream);

c.

FileOutputStream fstream =

new FileOutputStream("BinaryFile.dat", true);

DataOutputStream binaryOutputFile =

new DataOutputStream(fstream);

d.

IFileOutputStream fstream =

new FileOutputStream("BinaryFile.dat");

DataOutputStream binaryOutputFile =

new DataOutputStream(fstream, true);

45. What will the following code display?

String input = "99#7";

int number;

try

{

number = Integer.parseInt(input);

}

catch(NumberFormatException ex)

{

number = 0;

}

catch(RuntimeException ex)

{

number = 1;

}

catch(Exception ex)

{

number = -1;

}

System.out.println(number);

a.

-1

b.

0

c.

1

d.

99

Document Information

Document Type:
DOCX
Chapter Number:
11
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 11 Exceptions and Advanced File I/O
Author:
Tony Gaddis

Connected Book

Java Control Structures 7e Test Bank

By Tony Gaddis

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party