Q12
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.
What is an exception?
An object that signals the occurrence of an unusual event.
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
What happens if an exception is thrown by a statement inside a try block?
The remainder of the try block is skipped and then the code in the catch block is executed.
A statement opening a text file for reading or writing may generate a FileNotFoundException. T or F?
True
A try block must have a corresponding catch block. T or F?
True
Data in a file remains after program execution ends. T or F?
True
Java provides several predefined exception classes. T or F?
True
Which of the following is the correct way to open a text file called "myfile.txt" for reading?
inputStream = new Scanner(new File("myfile.txt"));
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?
outputStream = new PrintWriter(new FileOutputStream("myfile.txt", true));
If the statement data.saveToFile() throws a FileNotFoundException, which of the following is the correct way to handle this?
try{ // some code data.saveToFile(); // more code } catch (FileNotFoundException e) { // code to handle FileNotFoundException }