File IO and Exception Handling

File IO and Exception Handling 1) Insert the missing code in the following code fragment. This fragment is intended to read an input file. public stat...
Author: Rodger Stone
2 downloads 0 Views 211KB Size
File IO and Exception Handling 1) Insert the missing code in the following code fragment. This fragment is intended to read an input file. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = new File(inputFileName); Scanner in = _______________; . . . }

a) new b) new c) new d) new

Scanner(inputFileName) Scanner(outputFileName) Scanner(inputFile) Scanner(System.in)

3) Insert the missing code in the following code fragment. This fragment is intended to read a file and write to a file. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); PrintWriter out = _____________; . . . }

a) new b) new c) new d) new

PrintWriter(outputFileName) Scanner(outputFileName) PrintWriter(outputFile) Scanner(outputFile)

4) Which of the following statements about using the PrintWriter object is correct? a) If the output file already exists, the new data will be appended to the end of the file. b) If the output file does not exist, a FileNotFoundException will occur. c) If the output file already exists, the existing data will be discarded before new data are written into the file. d) If the output file does not exist, an IllegalArgumentException will occur.

6) Under which condition will the Scanner constructor generate a FileNotFoundException? a) If the input file cannot be opened due to a security error. b) If the input file does not exist. c) If the input file already exists, but has data in it. d) If the input file already exists, but is empty.

8) Which of the following is the correct syntax for creating a File object? a) File b) File c) File d) File

inFile inFile inFile inFile

= = = =

File("input.txt") new File("input.txt") File.open("input.txt") new File.open("input.txt")

9) Which of the following objects should be used for reading from a text file? a) Scanner b) ReadStream c) PrintStream d) ReadFile

10) Consider the following code snippet: public static void main(String[] args) throws FileNotFoundException

Which of the following statements about this code is correct? a) The main method is designed to catch and handle all types of exceptions. b) The main method is designed to catch and handle the FileNotFoundException. c) The main method should simply terminate if the FileNotFoundException occurs. d) The main method will not terminate if any exception occurs.

12) Which of the following statements about a PrintWriter object is true? a) A PrintWriter will be automatically closed when the program exits. b) Data loss may occur if a program fails to close a PrintWriter object before exiting. c) No data loss will occur if the program fails to close a PrintWriter before exiting. d) An exception will occur if the program fails to close a PrintWriter before exiting.

13) Your program will read in an existing text file. You want the program to terminate if the file does not exist. Which of the following indicates the correct code for the main method header? a) public b) public c) public d) public

static static static static

void void void void

main(String[] main(String[] main(String[] main(String[]

args) throws FileMissingException args) throws FileNotFoundException args) args) throws UnknownFileException

14) Consider the following code snippet: File inputFile = new File("input.txt");

You wish to read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this? a) Scanner b) Scanner c) Scanner d) Scanner

in in in in

= = = =

Scanner ("input.txt") new Scanner ("input.txt") Scanner.open(inputFile) new Scanner(inputFile)

15) Consider the following code snippet: PrintWriter out = new PrintWriter("output.txt");

Which of the following statements about the PrintWriter object is correct? a) If a file named "output.txt" already exists, an exception will occur. b) If a file named "output.txt" already exists, data will be added at the end of the file. c) If a file named "output.txt" already exists, existing data will be deleted before data are added to the file. d) If a file named "output.txt" already exists, a new file named "output_1.txt" will be created and used. 16) The PrintWriter class is an enhancement of the ____ class. a) Scanner b) ReadStream c) PrintStream d) File 17) Which of the following statements about reading and writing text files is correct? a) You use the Scanner class to read and write text files. b) You use the PrintWriter class to read and write text files. c) You use the Scanner class to read text files and the PrintWriter class to write text files. d) You use the Scanner class to write text files and the PrintWriter class to read text files.

18) Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt and write to an output file named dataOut.txt. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; String outputFileName = "dataOut.txt"; File inputFile = _________________; Scanner in = new Scanner(inputFile) . . . }

a) new b) new c) new d) new

File(inputFileName) File(outputFileName) File(inputFile) File(System.in)

21) Consider the following code snippet. Scanner inputFile = new Scanner("dataIn.txt");

Which of the following statements is correct? a) This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value. b) This code will create a new file named "dataIn.txt". c) This code will open an existing file named "dataIn.txt" for reading. d) This code will open a file named "dataIn.txt" for writing.

24) Consider the following code snippet. PrintWriter outputFile = new PrintWriter("payrollReport.txt");

Which of the following statements about the PrintWriter object is correct? a) If a file named "payrollReport.txt" already exists, an exception will occur. b) If a file named "payrollReport.txt" already exists, existing data will be deleted before new data is added to the file. c) If a file named "payrollReport.txt" already exists, new data will be added to the end of the file. d) If a file named "payrollReport.txt" already exists, a new file named "payrollReport_1.txt" will be created and used.

26) Your program must read in an existing text file. You want the program to terminate if the file does not exist. Which of the following indicates the correct code for the main method header? a) public b) public c) public d) public

static static static static

void void void void

main(String[] main(String[] main(String[] main(String[]

args) throws FileMissingException args) throws FileNotFoundException args) args) throws UnknownFileException

28) Insert the missing code in the following code fragment. This fragment is intended to read an input file named hoursWorked.txt. You want the program to terminate if the file does not exist. public static void main(String[] args) ______________ { File inputFile = new File("hoursWorked.txt"); Scanner in = new Scanner(inputFile); . . . }

a) catch FileMissingException b) catch FileNotFoundException c) throws FileMissingException d) throws FileNotFoundException

29) Insert the missing code in the following code fragment. This fragment is intended to write an output file named dataOut.txt that resides in a folder named reports on the C: drive of a Windows system. public static void main(String[] args) throws IOException { PrintWriter outputFile = _______; . . . }

a) new b) new c) new d) new

PrintWriter("c:/reports/dataOut.txt") PrintWriter("c://reports//dataOut.txt") PrintWriter("c:\reports\dataOut.txt") PrintWriter("c:\\reports\\dataOut.txt")

31) Your program wishes to open a file named C:\java\myProg\input.txt on a Windows system. Which of the following is the correct code to do this? a) inputFile b) inputFile c) inputFile d) inputFile

= = = =

new new new new

File("c:\java\myProg\input.txt"); File.open("c:\java\myProg\input.txt"); File("c:\\java\\myProg\\input.txt"); File.open("c:\\java\\myProg\\input.txt");

45) Which of the following statements about white space in Java is correct? a) In Java, white space includes spaces only. b) In Java, white space includes spaces and tab characters only. c) In Java, white space includes spaces, tab characters, and newline characters. d) In Java, white space includes spaces, tab characters, newline characters, and punctuation. 47) Consider the following code snippet: Scanner in = new Scanner(. . .); in.useDelimiter("[^0-9]+");

What characters will be ignored and not read in using this code? a) Only alphabetic characters will be ignored. b) Only numeric characters will be ignored. c) Only non-alphabetic characters will be ignored. d) Only non-numeric characters will be ignored.

48) Consider the following code snippet: Scanner in = new Scanner(. . .); in.useDelimiter("[^0-9A-Za-z]+");

What characters will be ignored and not read in using this code? a) Only alphabetic characters will be ignored. b) Only numeric characters will be ignored. c) Characters that are neither alphabetic nor numeric will be ignored. d) Both alphabetic and numeric characters will be ignored. 49) Insert the missing code in the following code fragment. This fragment is intended to read characters from a text file. Scanner in = new Scanner(. . .); in.useDelimiter(""); while (in.hasNext()) { char ch = ____________; System.out.println(ch); }

a) in.getNext() b) in.next() c) in.next.charAt(0) d) in.nextChar()

50) Which of the following patterns should be used for the delimiter to read one character at a time using a Scanner object's next method? a) Scanner in = new Scanner(. . .); in.useDelimiter("[^A-Za-z]+");

b) Scanner in = new Scanner(. . .); in.useDelimiter("[A-Za-z]+");

c) Scanner in = new Scanner(. . .); in.useDelimiter("[^0-9]+");

d) Scanner in = new Scanner(. . .); in.useDelimiter("");

Answer: d

52) The ____ method of the Character class will indicate if a character contains white space. a) isValid() b) getChar() c) hasNext() d) isWhiteSpace() 53) Consider the following code snippet: Scanner in = new Scanner(. . .); while (in.hasNextLine()) { String input = in.nextLine(); System.out.println(input); }

Which of the following statements about this code is correct? a) This code will read in an entire line from the file in each iteration of the loop. b) This code will read in the entire contents of the file in a single iteration of the loop. c) This code will read in a single word from the file in each iteration of the loop. d) This code will read in a single character from the file in each iteration of the loop. 61) You wish to use the Scanner class's nextInt() method to read in whole numbers. To avoid exceptions that would occur if the input is not a whole number, you should use the ____ method before calling nextInt. a) hasNext() b) hasNextInteger()

c) hasIntegerValue() d) hasNextInt()

73) In the hierarchy of Exception classes, the NumberFormatException class is a subclass of the ____ class. a) ArithmeticException. b) ClassCastException. c) IllegalArgumentException. d) IllegalStateException.

75) Which of the following statements about exception handling is correct? a) Statements that may cause an exception should be placed within a catch block. b) The main method of a Java program will handle any error encountered in the program. c) Statements that may cause an exception should be placed within a throws block. d) Statements that may cause an exception should be placed within a try block. 76) Which statement about handling exceptions is true? a) If an exception has no handler, the error will be ignored. b) Statements that may cause exceptions should be placed inside a catch clause. c) Statements to handle exceptions should be placed inside a try clause d) If an exception has no handler, the program will be terminated. 78) Insert the missing code in the following code fragment. This code is intended to open a file and handle the situation where the file cannot be found. try { String filename = . . .; Scanner in = new Scanner(new File(filename)); . . . } ___________________ { exception.printStackTrace(); }

a) catch b) catch c) catch d) catch

(IOException exception) (new exception (IOException)) (IllegalArgumentException exception) (IOException)

80) Which of the following statements about checked and unchecked exceptions is true?

a) Checked exceptions are handled by the Java runtime. b) The compiler ensures that the program is handling unchecked exceptions. c) The compiler ensures that the program is handling checked exceptions. d) All exceptions that are descendants of RunTimeException are checked exceptions.

83) If the current method in a program will not be able to handle an exception, what should be coded into the method? a) The throws clause should list the name of the method to which the exception should be passed. b) The method declaration should be enclosed in a try/catch block. c) The method should include a try/catch block for all possible exceptions. d) The throws clause should list the names of all exceptions that the method will not handle. 84) When writing a method, which of the following statements about exception handling is true? a) The throws clause must list all checked exceptions that this method may throw, and may also list unchecked exceptions. b) The throws clause must list all unchecked exceptions, and may also list checked exceptions that this method may throw. c) The throws clause must list all checked exceptions, but cannot list unchecked exceptions. d) The throws clause must list all unchecked exceptions, and cannot list checked exceptions.

89) Consider the following code snippet: PrintWriter outputFile = new PrintWriter(filename); writeData(outputFile); outputFile.close();

How can the program ensure that the file will be closed if an exception occurs on the writeData call? a) The program does not need to take any action, because the output file will be automatically closed when the exception occurs. b) The program should place the outputFile.close() statement within a try block to ensure that the file will be closed. c) It is not possible to ensure that the file will be closed when the exception occurs. d) The program should place the outputFile.close() statement within a finally clause of a try block to ensure that the file is closed.