Ch 12 - Exceptions and More about Stream IO

¡Supera tus tareas y exámenes ahora con Quizwiz!

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) 2, 3, 1, 4 (c) 4, 1, 3, 2 (d) 3, 1, 2, 4

(b) 2, 3, 1, 4 1. Exception 2. IllegalArgumentException 3. RuntimeException 4. Throwable

The following two statements can be written as: FileReader freader = new FileReader("InputFile.txt"); BufferedReader inputFile = new BufferedReader(freader); (a) BufferedReader inputFile = new FileReader("InputFile.txt"); (b) BufferedReader inputFile = new BufferedReader(new FileReader("InputFile.txt")); (c) FileReader freader = new BufferedReader("InputFile.txt"); (d) FileReader freader = new FileReader(new BufferedReader(inputFile));

(b) BufferedReader inputFile = new BufferedReader(new FileReader("InputFile.txt"));

The IllegalArgumentException class is derived from the RuntimeException class, and is therefore (a) A checked exception class (b) An unchecked exception class (c) Never used directly (d) None of the above

(b) an unchecked exception class

A(n) ____ is a section of code that gracefully responds to exceptions when they are thrown. (a) Thrown class (b) Default exception handler (c) Exception (d) Exception handler

(d) Exception handler

If a random access file contains a stream of characters, which of the following would you use to set the pointer on the fiftieth character? (a) file.seek(49); (b) file.seek(50); (c) file.seek(100); (d) file.seek(98);

(d) file.seek(98);

True/False A BufferedReader object cannot read data directly form a file; it must read its data from another object that provides an input stream.

True

True/False A PrintWriter object cannot write text directly to a file; it writes its data to another object's output stream.

True

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

True

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

True

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

True

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

What does the following statement do when it is executed? FileReader freader new Filereader("InputFile.txt"); (a) It creates a FileReader object and opens an input file called InputFile.txt (b) It closes an input file called InputFile.txt (c) It reads file header information for a file called InputFile.txt (d) It reads the first record of the file called InputFile.txt

(a) It creates a FileReader object and opens an input file called InputFile.txt

Unchecked exceptions are those that inherit from (a) The Error class or the RuntimeException class (b) The Error class or the Exception class (c) The Exception Class or the RuntimeException class (d) Only the Error class

(a) The Error class or the RuntimeException class

When you use a checked exception class, you must (a) Have a throws clause in the method heading (b) Override the default error method (c) Use each class only once in a method (d) Ensure that the error will occur at least once each time the program is executed

(a) have a throws clause in the method heading

How could the following method header be written better? public void copyFile(String str) throws IOException, EOFException (a) public void copyFile(String str) throws IOException (b) public void copyFile(String str) throws EOFException (c) public void copyFile(String str) throws EOFException, IOException (d) public void copyFile(String str) throws Exception, IOException, EOFException

(a) public void copyFile(String str) throws IOException

In a catch statement, what does the following code do? System.out.println(e.getMessage()); (a) It prints the programmer-defined message for an exception (b) It prints the default error message for an exception (c) It prints the code that caused the exception (d) It overrides the toString method

(b) It prints the default error message for an exception

All of the exceptions that you will handle are instances of classes that are derived from (a) RunTimeException (b) IOException (c) Error (d) Exception

(d) Exception

Classes that are derived 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 (b) For exceptions that are thrown when a critical error occurs, and the application program should try to handle them (c) For exceptions that are thrown when an IOException occurs, and the application program should not try to handle them (d) For exceptions that are thrown when an IOException error occurs, and the application program should try to handle them

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

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. Throwable 2. Exception 3. RuntimeException 4. NumberFormatException (a) 1, 2, 3, 4 (b) 2, 3, 1, 4 (c) 4, 1, 3, 2 (d) 3, 1, 2, 4

(a) 1, 2, 3, 4 1. Throwable 2. Exception 3. RuntimeException 4. NumberFormatException

The default error message can be retrieved using the ____ method. (a) getMessage() (b) getErrorMessage() (c) getDefaultMessage() (d) getDefaultErrorMessage()

(a) getMessage(0

The following catch statement can catch (Exception e) {...} (a) Handle all error codes by using polymorphic reference as a parameter in the catch clause (b) Handle all throwable objects by using polymorphic reference as a parameter in the catch clause (c) Handle all exceptions by using polymorphic reference as a parameter in the catch clause (d) Is an error since no objects are instantiated in the Exception class

(c) Handle all exceptions by using polymorphic reference as a parameter in the catch clause

When an exception is thrown, (a) It must be handled by the method that throws it (b) It must be handled by the method that throws it or one of the calling methods in the stack trace (c) It must be handled by the program or by the default exception handler (d) It may be ignored

(c) It must be handled by the program or by the default exception handler

____ is an abstract class that all the other character stream-reading classes are derived from. (a) InputStreamReader (b) BufferedReader (c) Reader (d) FileReader

(c) Reader

Which of the following statements will open the file InputFile.txt that is on the C: drive? (a) FileReader freader new FileReader("C:\InputFile.txt); // on a Windows computer (b) FileReader freader new FileReader("C:\InputFile.txt); // on a UNIX or Linux computer (c) FileReader freader new FileReader("/c/InputFile.txt"); // on a Windows computer (d) FileReader freader new FileReader("C:\\InputFile.txt); // on a Windows computer

(d) FileReader freader = new FileReader("C:\\InputFile.txt); // on a windows computer

A stream of unformatted binary data is called (a) An inputstream (b) An outputstream (c) A character stream (d) A bytestream

(d) a bytestream

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(4); (b) file.seek(5); (c) file.seek(9); (d) file.seek(8);

(d) file.seek(8);

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

False

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

False

True/False 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 go execute that method.

True

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

True

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

(a) FileNotFoundException will be thrown

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 BufferedReader and PrintWriter methods (c) The FileReader and Writer class methods (d) The System.In and System.Out methods

(a) Methods that use UTF-8 encoding

FileWriter fwriter new FileWriter("BookData.txt"); PrintWriter bookOutFile new PrintWriter(fwriter); (a) PrintWriter bookOutFile new PrintWriter(new FileWriter("BookData.txt")); (b) PrintWriter bookOutFile new FileWriter("BookData.txt); (c) FileWriter fwriter new PrintWriter(fwriter); (d) FileWriter fwriter new FileWriter(new PrintWriter("BookData.txt"));

(a) PrintWriter bookOutFile = new PrintWriter(new FileWriter("BookData.txt"));

A(n) ____ is an object that is generated in memory as the result of an error or an unexpected event. (a) Exception handler (b) Exception (c) Default exception handler (d) Error message

(b) Exception

Given the following constructor, what must be true about the calling program? 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; } (a) A throws clause should be added to the constructor header (b) Classes derived from the Exception class should be created for each of the exceptions in the constructor (c) The calling program must handle the throw conditions of the constructor (d) All of the above

(d) All of the above

The catch clause (a) Immediately follows the try clause (b) Starts with the word catch followed by a parameter list in parentheses containing an ExceptionType parameter variable (c) Contains code to gracefully handle the exception type listed in the parameter list (d) All of the above

(d) All of the above

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 text data from the Input.dat file (b) The inFile variable will reference an object that is able to read binary data from the Input.dat file (c) The inFile variable will reference an object that is able to read formatted 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

(b) 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); (a) The outputFile variable will reference an object that is able to write text data 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 write formatted binary data to the Output.dat file (d) The outputFile variable will reference an object that is able to write a random access file 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

If the program does not handle an exception, (a) The exception is ignored (b) The program is halted and the default exception handler handles the exception (c) The program must handle the exception (d) This will cause a compilation error

(b) The program is halted and the default exception handler handles the exception

If "14t8" is entered for input in the following code, what does the program do? while (input != null) { try { totalIncome += Double.parsedouble(input); months++; } catch(NumberFormatException e) { System.out.println("Non-numeric data encountered in the file: " + e.getMessage()); } input = inputFile.readLine(); } (a) input will be converted to a double and added to totalIncome, months will be incremented by 1, and the while statement will be repeated until a null is entered (b) input will cause a NumberFormatError, the catch clause will be executed, then the program will continue by asking for the next input value (c) input will cause a NumberFormatError, the catch clause will be executed, then the terminate (d) input will cause a NumberFormatError, the catch clause will be executed, then the program will resume with the statement following the while statement

(b) input will cause a NumberFormatError, the catch clause will be executed, then the program will continue by asking for the next input value

What will be the value of totalIncome after the following values are entered for input: null, 2.5, 8.5, 3.0, 5.5? double totalIncome = 0.0; input = inputFile.readLine(); while (input != null) { try { totalIncome ++; Double.parsedouble(input); months++; } catch(NumberFormatException e) { System.out.println("Non-numeric data encountered in the file: " + e.getMessage()); } finally { totalIncome = 35.5; } input = inputFile.readLine(); }

(c) 35.5

If you want to append data to the existing binary file, BinaryFile.dat, use the following statements 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) FileOutputStream fstream new FileOutputStream("BinaryFile.dat"); DataOutputStream binaryOutputFile new DataOutputStream(fstream, true);

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

Given the following constructor, what must be true about the calling program? 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; } (a) It must call the constructor with valid data (b) It must contain an inner class that is derived from the IOException class (c) It must handle the throw conditions of the constructor (d) All of the above

(c) It must handle the throw conditions of the constructor

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) A FileNotFoundException will be thrown (b) An IOExcepton will be thrown (c) The file IOData.dat will be created (d) This is a critical error, the program will stop execution

(c) The file IOData.dat will be created

When an exception is thrown by code in the try block, the JVM begins searching the try/catch statement for a catch clause that can handle it and passes control of the program to (a) Each catch clause that can handle the exception (b) The last 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

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

(c) The program resumes at the statement that immediately follows the try/catch construct

If a method does not handle a possible exception, what must the method have? (a) A catch clause in its header (b) A try/catch clause in its header (c) A try clause in its header (d) A throws clause in its header

(d) A throws clause in the header

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 (a) or (b) (d) Either (a) or (b)

(d) Either (a) or (b) (a - handle the exception, b- have a throws clause listed in the method header)

If "14t8" is entered for input in the following code, what does the program do? while (input != null) { try { totalIncome += Double.parsedouble(input); months++; } catch(NumberFormatException e) { System.out.println("Non-numeric data encountered in the file: " + e.getMessage()); input = inputFile.readLine(); } } (a) input will be converted to a double and added to totalIncome, months will be incremented by 1, and the while statement will be repeated until a null is entered (b) input will cause a NumberFormatError, the catch clause will be executed, then the program will continue by asking for the next input value (c) input will cause a NumberFormatError, the catch clause will be executed, then the terminate (d) input will cause a NumberFormatError, the catch clause will be executed, then the while statement will be repeated until a value that can be parsed or a null is entered. If a null is entered, the program continues with the statement after the while statement; otherwise, this will be an endless loop.

(d) input will cause a NumberFormatError, the catch clause will be executed, then the while statement will be repeated until a value that can be parsed or a null is entered. If a null is entered, the program continues with the statement after the while statement; otherwise, this will be an endless loop


Conjuntos de estudio relacionados

The Current Ethernet Specifications

View Set

K4. UNIT 9: WHAT'S THE FASTEST ANIMAL IN THE WORLD?

View Set

Real Estate Principles Summary Quiz

View Set

La Celula, Su estructura y funcion.

View Set

Chapter 12: Production and Growth- Macroeconomic

View Set