What is an exception?
An object that signals the occurrence of an unusual event.
A try
block must have corresponding catch
block.
True
What happens if an exception is thrown by a statement inside a try
block?
The remainder of the
try
block is skipped and then code in thecatch
block is executed.
Java provides several predefined exception classes.
True
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 }
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.
Data in a file remains after program execution ends
True
Which Java class is the preferred class to use for writing to a text file?
PrintWriter
Which Java class is the preferred class to use for reading from a text file?
Scanner
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));
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"));
A statement opening a text file for reading or writing may generate a FileNotFoundException
.
True