Java: Chapter 11 Quiz

Ace your homework & exams now with Quizwiz!

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

A) 3.jpg

If a method does not handle a possible checked exception, what must the method have? A) A throws clause in its header B) A try catch clause in its header C) A catch clause in its header D) A try clause in its header

A) A throws clause in its header

If the code in a method can potentially throw a checked exception, then that method must: A) either handle the exception or have a throws clause listed in the method header B) have a throws clause listed in the method header C) handle the exception D) neither handle the exception nor have a throws clause listed in the method header

A) either handle the exception or have a throws clause listed in the method header

A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event. A) exception B) exception handler C) error message D) default exception handler

A) exception

In a try/catch construct, after the catch statement is executed: A) the program resumes at the statement that immediately follows the try / catch construct B) the program terminates C) the program resumes at the first statement of the try statement D) the program returns to the statement following the statement in which the exception occurred

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

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) 3, 1, 2, 4 B) 4, 3, 2, 1 C) 2, 3, 1, 4 D) 4, 1, 3, 2

B) 4, 3, 2, 1

Under 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");

B) 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? IMAGE A) All of these B) It must handle all of the possible exceptions thrown by the constructor or have its own throws clause specifying them. C) It must contain an inner class that extends the IOException class. D) It must call the constructor with valid data or a compiler error will occur.

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

What is demonstrated by the following code? try { (try block statements . . .) } catch(NumberFormatException | IOException ex) { respondToError(); } A catch clause that can handle either exception type, but not both B) Multi-catch, a catch clause that can handle more than one exception, beginning in Java 7 C) A conditional catch clause prototype that uses an overloaded OR operator to bind exception types D) This code is not supported in any version of Java, an error will result.

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

If the IOData.dat file does not exist, what will happen when the following statement is executed? RandomAccessFile randomFile = new RandomAccessFile("IOData.dat", "rw"); A) An IOExcepton will be thrown. B) The file IOData.dat will be created. C) A FileNotFoundException will be thrown. D) This is a critical error, the program will stop execution.

B) The file IOData.dat will be created.

An exception's default error message can be retrieved using this method. A) getDefaultErrorMessage() B) getMessage() C) getDefaultMessage() D) getErrorMessage()

B) getMessage()

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(nuber); A) 997 B) 1 C) 0 D) 99

C) 0

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) 75.0 C) 35.5 D) .0

C) 35.5

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) ExceptionMessage B) ParseIntError C) NumberFormatException D) FileNotFoundException

C) NumberFormatException

What will be the result of the following statements? FileInputStream fstream = new FileInputStream("DataIn.dat"); DataInputStream inFile = new DataInputStream(fstream); A) The inFile variable will reference an object that is able to read only text data from the Input.dat file. B) The inFile variable will reference an object that is able to write binary data to 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 read random access 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.

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? A) file.seek(9); B) file.seek(5); C) file.seek(8); D) file.seek(4);

C) file.seek(8);

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: 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) If there are two or more catch clauses that can handle the exception, the program halts.

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

If the program does not handle an unchecked exception: A) the program must handle the exception B) this will cause a compilation error C) the program is halted and the default exception handler handles the exception D) the exception is ignored

C) the program is halted and the default exception handler handles the exception

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 A) 1, 2, 3, 4 B) 4, 1, 3, 2 C) 3, 1, 2, 4 D) 2, 3, 1, 4

D) 2, 3, 1, 4

In a catch statement, what does the following code do? System.out.println(e.getMessage()); A) It overrides the toString method. B) It prints the code that caused the exception. C) It prints the stack trace. D) It prints the error message for an exception.

D) It prints the error message for an exception.

The exception classes are in packages in the ________. A) JVM B) Ex C) Compiler D) Java API

D) Java API

In order for an object to be serialized, its class must implement this interface. A) Writable B) ObjectOutputStream C) Serial D) Serializable

D) Serializable

All exceptions are instances of classes that extend this class. A) Exception B) Error C) RunTimeException D) Throwable

D) Throwable

To serialize an object and write it to the file, use this method of the ObjectOutputStream class. A) SerializeAndWrite B) Serialize C) SerializeObject D) WriteObject

D) WriteObject

The IllegalArgumentException class extends the RuntimeException class, and is therefore: A) never used directly B) a checked exception class C) None of these D) an unchecked exception class

D) an unchecked exception class

The following catch statement can: catch (Exception e) {...} A) handle all exceptions that are instances of the Exception class, but not a subclass of Exception B) handle all throwable objects by using polymorphic reference as a parameter in the catch clause C) is an error since no objects are instantiated in the Exception class D) handle all exceptions that are instances of the Exception class or a subclass of Exception

D) handle all exceptions that are instances of the Exception class or a subclass of Exception

The ability to catch multiple types of exceptions with a single catch is known as ________, and was introduced in Java 7. A) exception trapping B) super catch C) compound catch D) multi-catch

D) multi-catch

Unchecked exceptions are those that inherit from: A) the Error class or the Exception class B) the Exception Class or the RuntimeException class C) only the Error class D) the Error class or the RuntimeException class

D) the Error class or the RuntimeException class

In a multi-catch, (introduced in Java 7) the exception types are separated in the catch clause by this symbol: A) * B) & C) ? D) |

D) |

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

False

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

False

The throws clause causes an exception to be thrown. True/False

False

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

True

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. True/False

True

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/False

True

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

True

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

True

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/False

True

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

True

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

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/False

True

The catch clause: A) All of these B) contains code to gracefully handle the exception type listed in the parameter list C) follows the try clause D) starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable

A) All of these

All of the exceptions that you will handle are instances of classes that extend this class. A) Exception B) Error C) RunTimeException D) IOException

A) Exception

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."); } A) Because you can have only one catch clause in a try statement B) Because NumberFormatException inherits from IllegalArgumentException. The code should handle NumberFormatException before IllegalArgumentException C) Because the Integer.parseInt method does not throw an IllegalArgumentException D) Because the Integer.parseInt method does not throw a NumberFormatException)

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

What will be the result of the following code? FileOutputStream fstream new FileOutputStream("Output.dat"); DataOutputStream outputFile = new DataOutputStream(fstream); A) The outputFile variable will reference an object that is able to write to Output.dat as a random access 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 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.

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 (numberOrderedOfBook < 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) The calling method must handle the exceptions thrown in the constructor, or have its own throws clause specifying them. C) All of these D) Classes extending the Exception class should be created for each of the custom exceptions that are thrown in the constructor.

C) All of these

To write data to a binary file you create objects from the following classes: A) BinaryFileWriter and BinaryDataWriter B) File and Scanner C) FileOutputStream and DataOutputStream D) File and PrintWriter

C) FileOutputStream and DataOutputStream

If your code does not handle and exception when it is thrown, this prints an error message and crashes the program. A) try statement B) multi-catch C) Java error handler D) default exception handler

D) default exception handler

This is a section of code that gracefully responds to exceptions when they are thrown. A) Exception handler B) Exception C) Thrown class D) Default exception handler

A) Exception handler

To read data from a binary file you create objects from the following classes: A) FileInputStream and DataInputStream B) File and PrintWriter C) BinaryFileReader and Binary DataReader D) File and Scanner

A) FileInputStream and DataInputStream

If you want to append data to the existing binary file, BinaryFile.dat, use the following statements to open the file. A) Image 3.jpg

A) Image 3.jpg

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 method D) The System.In and System.Out methods

A) Methods that use UTF-8 encoding

The try statement may have an optional ________ clause, which must appear after all of the catch clauses. A) finally B) abort C) default D) try-again

A) finally

Classes that inherit from the Error class are: A) for exceptions that are thrown when an IOException occurs, and the application program should not try to handle them B) for exceptions that are thrown when a critical error occurs, and the application program should not try to handle them C) for exceptions that are thrown when a critical error occurs, and the application program should try to handle them D) for exceptions that are thrown when an IOException error occurs, and the application program should try to handle them

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


Related study sets

Writing and Graphing Equations in Two Variables

View Set

FIN205 Topic 7: Valuation of intangible assets

View Set

Final test Introduction hospitality

View Set

II Lecture Chapter 18 Certification Style Exam Quiz

View Set

Chapter 43 Trauma Systems and Mechanism of Injury

View Set

Emergency Medical Responder: First On Scene Chapter 1 & 2

View Set