Java Chapter 11

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

In a catch statement, what does the following code do? System.out.println(e.getMessage());

It prints the error message for an exception.

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

unchecked and checked

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

False

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

True

The throws clause causes an exception to be thrown.

False

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

FileOutputStream and DataOutputStream

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

Serializable

If the data.dat file does not exist, what will happen when the following statement is executed? RandomAccessFile randomFile = new RandomAccessFile("data.dat", "rw");

The file, data.dat, will be created.

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

Throwable

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.

True

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

bytes

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?

file.seek(8);

The following catch statement can __________. catch (Exception e) {...}

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

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

have a throws clause in the method header

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

multi-catch

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

try block

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

NumberFormatException

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

Question options: handle the exception have a throws clause listed in the method header neither of these --=-=either of these

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

True

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

exception handler

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

35.5

Unchecked exceptions are those that inherit from the __________.

Error class or the RuntimeException class

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

Exception

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

False

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

FileInputStream and DataInputStream

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?

FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", true); DataOutputStream binaryOutputFile = new DataOutputStream(fstream);

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

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

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; }

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

What will be the result of the following statements? FileInputStream fstream = new FileInputStream("Input.dat"); DataInputStream inFile = new DataInputStream(fstream);

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

What will be the result of the following statements? FileOutputStream fstream = new FileOutputStream("Output.dat"); DataOutputStream outputFile = new DataOutputStream(fstream);

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

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

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

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.

True

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

True

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

True

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

True

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

True

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

True

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.

True

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

WriteObject

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

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

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

an unchecked exception class

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

default exception handler

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

exception

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

finally

When an exception is thrown __________.

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

The catch clause __________.

starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable follows the try clause contains code to gracefully handle the exception type listed in the parameter list The catch clause does all of these

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

the exception will have a null message

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 __________.

the first catch clause that can handle the exception

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

Exception

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

a throws clause in its header

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

getMessage()

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.

False

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

methods that use UTF-8 encoding

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

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

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; }

There is an error: a throws clause should be added to the constructor header. Classes extending the Exception class should be created for each of the custom exceptions that are thrown in the constructor. A FileNotFoundException will be thrThe calling method must handle the exceptions thrown in the constructor, or have its own throws clause specifying them. All of these are true.

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

binary file

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

|

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

getMessage


Ensembles d'études connexes

Chapter 31: Inflation, Disinflation, and Deflation

View Set

Interview: Why Do Some Brains Enjoy Fear?

View Set

Unit 3 Pop Quiz Criminal Justice

View Set

Vocabulary Workshop Second Course - Lesson 23

View Set

Strategic Management - Chapter 8

View Set

Chapter 19 Multiple Choice (Blood Vessels)

View Set