WorksheetsFile I/O and Exceptions v2
Total questions: 22
Worksheet time: 11mins
The _______________ package contains the file handling classes such as PrintWriter and BufferedReader.
java.Writer
java.stream
java.io
java.FileWriter
A text file can only contain
primitive data types
integers
characters
reference types
What type of stream can you use to send data from an application to a network?
input stream
output stream
cloud stream
jet stream
What is it called when you combine several types of streams to get the functionality you need?
buffering
flushing
layering
queuing
What is it called when data is stored in a block of memory before it’s moved from memory to a file or database?
buffering
flushing
layering
queuing
Which class defines the most general type of exception for file input and output?
FileException
IOException
FileIOException
DataException
To write to a file more efficiently, what type of object should you include in the stream?
FileReader
PrintWriter
BufferedWriter
FileWriter
Which of the following classes is not used to connect an output stream to a file?
PrintWriter
BufferedWriter
DataWriter
FileWriter
What exception is thrown by methods of the BufferedReader class?
NullPointerException
IOException
FileException
BufferFullException
To declare that a method throws a checked exception, you must
add a throws clause to the method declaration.
code a throws clause within the method body
code a throw statement within the method body
code a throw statement before the method body
To throw an exception, you can use the
throw keyword
raise keyword
throwException() method
raiseException() method
The following method is not available to all exceptions
getMessage()
toString()
printStackTrace()
printDebugInfo()
Unchecked exceptions are derived from which class?
UncheckedException
RuntimeException
UncheckedError
RuntimeError
Both checked and unchecked exceptions
can be caught in a try/catch statement
are found during compile time
are derived from the Error class
require an exception handler
When writing I/O code that may throw an IOException, why do you need to handle this exception or declare that the method throws this exception?
Because the IOException is a runtime exception
Because the IOException is a checked exception
Because the IOException is a default exception
You don’t need to handle this exception or declare that the method throws it.
The try-with-resources statement doesn’t need a finally clause because it
uses a multi-catch block to catch all exceptions
implements the AutoCloseable interface
automatically closes all declared resources
catches all exceptions that are thrown when you try to close a resource
When coding catch blocks, you should always code them in what order?
Most specific exception to least specific
Least specific exception to most specific
Most important exception to least important
Order doesn’t matter
Given an Exception object named e, which of the following statements prints the object’s class name and message to the error output stream?
System.err.println(e);
System.error.println(e);
System.out.println(err);
System.out.println(error.toString(e));
If the following code is located in a method named readData(), what must a method that calls readData() do?
Handle the FileNotFoundException or declare that it throws this exception.
Handle the IOException or declare that it throws this exception
Handle both the FileNotFoundException and the IOException
Doesn’t have to throw or handle any exceptions
In which of the following situations would you typically not throw an exception from a method?
When you want to test an exception handler
When you need to perform some processing before the exception is handled by the calling method
When invalid arguments are passed to the method
When the exception is handled by the method
Where is the IOException handled?
main()
methodA()
methodB()
It’s not handled.
What happens if a FileNotFoundException occurs in the code that follows?
“FileNotFoundException” is printed to the console and the BufferedReader and FileReader objects are left open
“FileNotFoundException” is printed to the console and the BufferedReader and FileReader objects are closed
“FileNotFoundException” and “IOException” are printed to the console, and the BufferedReader and RileReader objects are left open
“FileNotFoundException” and “IOException” are printed to the console, and the BufferedReader and RileReader objects are closed
