Chapter 11 BCIS 3680

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

An exception's default error message can be retrieved using this method. a. getMessage() b. getErrorMessage() c. getDefaultMessage() d. getDefaultErrorMessage()

a

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 b. 2, 3, 1, 4 c. 4, 1, 3, 2 d. 3, 1, 2, 4

a

The ability to catch multiple types of exceptions with a single catch is known as ____________, and was introduced in Java 7. a. multi-catch b. super catch c. Exception trapping d. compound catch

a

The exception classes are in packages in the _____________. a. Java API b. JVM c. Compiler d. Ex class

a

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

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 b. A catch clause that can handle either exception type, but not both 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.

a

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

a

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

All exceptions are instances of classes that extend this class. a. RunTimeException b. Throwable c. Error d. Exception

b

If the program does not handle an unchecked 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

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

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

b

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

b

The IllegalArgumentException class extends 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

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

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 a NumberFormatException. d. Because the Integer.parseInt method does not throw an IllegalArgumentException.

b

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; } 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 the above.

c

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

In order for an object to be serialized, its class must implement this interface. a. Serial b. Writable c. Serializable d. ObjectOutputStream

c

The following catch statement can: catch (Exception e) {...} a. can 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. handle all exceptions that are instances of the Exception class or a subclass of Exception. d. is an error since no objects are instantiated in the Exception class.

c

To write data to a binary file you create objects from the following classes: a. File and PrintWriter b. File and Scanner c. FileOutputStream and DataOutputStream d. BinaryFileWriter and BinaryDataWriter

c

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. 99 b. 997 c. 0 d. 1

c

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

If a method does not handle a possible checked 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

This 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

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 b. ParseIntError c. ExceptionMessage d. FileNotFoundException

a

To read data from a binary file you create objects from the following classes: a. FileInputStream and DataInputStream b. File and PrintWriter c. File and Scanner d. BinaryFileReader and BinaryDataReader

a

When you write a method that throws a checked exception, you must: a. have a throws clause in the method header. 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

The try statement may have an optional ____________ clause, which must appear after all of the catch clauses. a. try-again b. finally c. default d. abort

b

To serialize an object and write it to the file, use this method of the ObjectOutputStream class. a. SerializeObject b. WriteObject c. Serialize d. SerializeAndWrite

b

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 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 to Output.dat as a random access file.

b

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

If your code does not handle and exception when it is thrown, this prints an error message and crashes the program. a. Java error handler b. multi-catch c. default exception handler d. try statement

c

In a multi-catch, (introduced in Java 7) the exception types are separated in the catch clause by this symbol: a. * b. ? c. | d. &

c

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

c

When an exception is thrown: a. it must always be handled by the method that throws it. b. the program terminates even if the exception is handled. c. it must be handled by the program or by the default exception handler. d. it may be ignored.

c

The catch clause: a. 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

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

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

All of the exceptions that you will handle are instances of classes that extend this class. a. RunTimeException b. IOException c. Error d. Exception

d

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. The calling method must handle the exceptions thrown in the constructor, or have its own throws clause specifying them. d. All of the above.

d

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

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

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

d


संबंधित स्टडी सेट्स

California Real Estate Chapter 7

View Set

US History Exam Semester 1 (final)

View Set

GS ENVS 302 CH 12 Climate Change

View Set

Communication Module 5 Test Review

View Set