logo

APSU Notes


Quiz 12

Question 1

What is an exception?

An object that signals the occurrence of an unusual event.

Question 2

A try block must have corresponding catch block.

True

Question 3

What happens if an exception is thrown by a statement inside a try block?

The remainder of the tryblock is skipped and then code in the catchblock is executed.

Question 4

Java provides several predefined exception classes.

True

Question 5

If the statement data.saveToFile() throws a FileNotFoundException, which of the following is the correct way to handle this?

1
2
3
4
5
6
7
8
9
10
try
{
  // some code
  data.saveToFile();
  // more code
}
catch (FileNotFoundException e)
{
  // code to handle FileNotFoundException
}

Question 6

What is the difference between an input stream and an output stream?

An input stream reads data from a file or console and sends it to a program. An output stream takes data from a program and writes it to a file or console.

Question 7

Data in a file remains after program execution ends

True

Question 8

Which Java class is the preferred class to use for writing to a text file?

PrintWriter

Question 9

Which Java class is the preferred class to use for reading from a text file?

Scanner

Question 10

Which of the following statements opens a text file called "myfile.txt" so that new text can be appended to the existing contents of the file?

1
outputStream = new PrintWriter(new FileOutputStream("myfile.txt", true));

Question 11

Which of the following is the correct way to open a text file called "myfile.txt" for reading?

1
inputStream = new Scanner(new File("myfile.txt"));

Question 12

A statement opening a text file for reading or writing may generate a FileNotFoundException.

True