Chapter 11 its

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

C. Serializable

This is a section of code that gracefully responds to exceptions when they are thrown

D. Exception handler

Classes that inherit from the Error class are:.

A. for exceptions that are thrown when a critical error occurs, and the application program should not try to handle them.

All exceptions are instances of classes that extend this class

B. Throwable

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

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

In a multi-catch, (introduced in Java 7) the exception types are separated in the catch clause by this symbol:

C. |

The catch clause:

All of the above.

When an exception is thrown:

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

The throws clause causes an exception to be thrown.

F

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

T

If, within one try statement you want to have catch clauses that catch exceptions of the following types, in which order should they appear in your program? (1) Throwable (2) Exception (3) RuntimeException (4) NumberFormatException

A. 4, 3, 2, 1

To read data from a binary file you create objects from the following classes:

A. FileInputStream and DataInputStream

The exception classes are in packages in the _____________.

A. Java API

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

An exception's default error message can be retrieved using this method.

A. getMessage()

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

D. Either A or B

Unchecked exceptions are those that inherit from:

A. a. the Error class or the RuntimeException class.

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

T

To write data to a binary file you create objects from the following classes:

C. FileOutputStream and DataOutputStream

If you want to append data to the existing binary file, BinaryFile.dat, use the following statements to open the file.

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

Look at the following code: FileInputStream fstream = new FileInputStream("MyInfo.dat"); DataInputStream inputFile = new DataInputStream(fstream); This code can also be written as:

B. DataInputStream inputFile = new DataInputStream(new FileInputStream("MyInfo.dat"));

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

C. Default exception handler

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

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

If a random access file contains a stream of characters, which of the following would you use to set the pointer on the fifth character?

D. file.seek(8);

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

T

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

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

Beginning in Java 7, multi-catch can reduce a lot of duplicated code in a try statement that needs to catch multiple exceptions, but perform the same operation for each one.

T

What is demonstrated by the following code? try { (try block statements . . .) } catch(NumberFormatException | IOException ex) { respondToError(); }

A. Multi-catch, a catch clause that can handle more than one exception, beginning in Java 7

If the program does not handle an unchecked exception:

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

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

C. 0

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.

T

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

A. NumberFormatException

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

B. WriteObject

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

C. 35.5

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

F

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

D. All of the above

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

D. exception

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.

T

When you write a method that throws a checked exception, you must:

A. have a throws clause in the method header.

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

T

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

C. The file IOData.dat will be created

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

D. A throws clause in its header

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

F

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

A. multi catch

If, within one try statement you want to have catch clauses of the following types, in which order should they appear in your program: (1) Exception (2) IllegalArgumentException (3) RuntimeException (4) Throwable

B. 2 3 1 4

Why does the following code cause a compiler error? try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) { System.out.println(str + " is not a number."); }

B. Because NumberFormatException inherits from IllegalArgumentException. The code should handle NumberFormatException before IllegalArgumentException.

The IllegalArgumentException class extends the RuntimeException class, and is therefore:

B. an unchecked exception class

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

B. exception

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

B. finally

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

B.It prints the error message for an exception.

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

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

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) throws BlankISBN, NegativePrice, NegativeNumberOrdered { 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; }

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

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

C. handle all exceptions that are instances of the Exception class or a subclass of Exception.

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

C. the first catch clause that can handle the exception.

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

T

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.

T


Set pelajaran terkait

Section 10: Secure Software Development

View Set

APUSH Chapter 5 Identification and Cause and Effect

View Set

Chp 18 Tenant-Landlord Relationships

View Set

Principles of Biology 1 Final Exam

View Set

Geography grade 8 module 5 week 10

View Set

ABEKA 7th Grade Vocabulary Spelling Quiz List #21

View Set