wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

File I/O and Exceptions v2

Total questions: 22

Worksheet time: 11mins

Name
Class
Date
1.

The _______________ package contains the file handling classes such as PrintWriter and BufferedReader.

a)

java.Writer

b)

java.stream

c)

java.io

d)

java.FileWriter

2.

A text file can only contain

a)

primitive data types

b)

integers

c)

characters

d)

reference types

3.

What type of stream can you use to send data from an application to a network?

a)

input stream

b)

output stream

c)

cloud stream

d)

jet stream

4.

What is it called when you combine several types of streams to get the functionality you need?

a)

buffering

b)

flushing

c)

layering

d)

queuing

5.

What is it called when data is stored in a block of memory before it’s moved from memory to a file or database?

a)

buffering

b)

flushing

c)

layering

d)

queuing

6.

Which class defines the most general type of exception for file input and output?

a)

FileException

b)

IOException

c)

FileIOException

d)

DataException

7.

To write to a file more efficiently, what type of object should you include in the stream?

a)

FileReader

b)

PrintWriter

c)

BufferedWriter

d)

FileWriter

8.

Which of the following classes is not used to connect an output stream to a file?

a)

PrintWriter

b)

BufferedWriter

c)

DataWriter

d)

FileWriter

9.

What exception is thrown by methods of the BufferedReader class?

a)

NullPointerException

b)

IOException

c)

FileException

d)

BufferFullException

10.

To declare that a method throws a checked exception, you must

a)

add a throws clause to the method declaration.

b)

code a throws clause within the method body

c)

code a throw statement within the method body

d)

code a throw statement before the method body

11.

To throw an exception, you can use the

a)

throw keyword

b)

raise keyword

c)

throwException() method

d)

raiseException() method

12.

The following method is not available to all exceptions

a)

getMessage()

b)

toString()

c)

printStackTrace()

d)

printDebugInfo()

13.

Unchecked exceptions are derived from which class?

a)

UncheckedException

b)

RuntimeException

c)

UncheckedError

d)

RuntimeError

14.

Both checked and unchecked exceptions

a)

can be caught in a try/catch statement

b)

are found during compile time

c)

are derived from the Error class

d)

require an exception handler

15.

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?

a)

Because the IOException is a runtime exception

b)

Because the IOException is a checked exception

c)

Because the IOException is a default exception

d)

You don’t need to handle this exception or declare that the method throws it.

16.

The try-with-resources statement doesn’t need a finally clause because it

a)

uses a multi-catch block to catch all exceptions

b)

implements the AutoCloseable interface

c)

automatically closes all declared resources

d)

catches all exceptions that are thrown when you try to close a resource

17.

When coding catch blocks, you should always code them in what order?

a)

Most specific exception to least specific

b)

Least specific exception to most specific

c)

Most important exception to least important

d)

Order doesn’t matter

18.

Given an Exception object named e, which of the following statements prints the object’s class name and message to the error output stream?

a)

System.err.println(e);

b)

System.error.println(e);

c)

System.out.println(err);

d)

System.out.println(error.toString(e));

19.

If the following code is located in a method named readData(), what must a method that calls readData() do?

a)

Handle the FileNotFoundException or declare that it throws this exception.

b)

Handle the IOException or declare that it throws this exception

c)

Handle both the FileNotFoundException and the IOException

d)

Doesn’t have to throw or handle any exceptions

20.

In which of the following situations would you typically not throw an exception from a method?

a)

When you want to test an exception handler

b)

When you need to perform some processing before the exception is handled by the calling method

c)

When invalid arguments are passed to the method

d)

When the exception is handled by the method

21.

Where is the IOException handled?

a)

main()

b)

methodA()

c)

methodB()

d)

It’s not handled.

22.

What happens if a FileNotFoundException occurs in the code that follows?

a)

“FileNotFoundException” is printed to the console and the BufferedReader and FileReader objects are left open

b)

“FileNotFoundException” is printed to the console and the BufferedReader and FileReader objects are closed

c)

“FileNotFoundException” and “IOException” are printed to the console, and the BufferedReader and RileReader objects are left open

d)

“FileNotFoundException” and “IOException” are printed to the console, and the BufferedReader and RileReader objects are closed