Flashcards
CSE 110 Chapter 07 Review
Cards in this deck (50)
- 11) 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 Scanner(inputFileName) b) new Scanner(outputFileName) c) new Scanner(inputFile) d) new Scanner(System.in)c
- 22) Insert the missing code in the following code fragment. This fragment is intended to read an input file. public static void main(String[] args) __________________ { String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); . . . } a) extends FileNotFoundException b) throws FileNotFoundException c) inherits FileNotFoundException d) catches FileNotFoundExceptionb
- 33) 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 PrintWriter(outputFileName) b) new Scanner(outputFileName) c) new PrintWriter(outputFile) d) new Scanner(outputFile)a
- 44) 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.c
- 55) Under which condition will the PrintWriter constructor generate a FileNotFoundException? a) If the output file cannot be opened or created due to a security error. b) If the output file does not exist. c) If the output file already exists, but has data in it. d) If the output file already exists, but is empty.a
- 66) 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.b
- 77) Which of the following statements reflects the textbook’s recommendations about closing files? a) Both the input and the output file do not need to be explicitly closed in the program. b) Only the input file must be explicitly closed in the program. c) Only the output file must be explicitly closed in the program. d) Both the input and the output file should be explicitly closed in the program.d
- 88) Which of the following is the correct syntax for creating a File object? a) File inFile = File("input.txt") b) File inFile = new File("input.txt") c) File inFile = File.open("input.txt") d) File inFile = new File.open("input.txt")b
- 99) Which of the following objects should be used for reading from a text file? a) Scanner b) ReadStream c) PrintStream d) ReadFilea
- 1010) 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.c
- 1111) Consider the following code snippet: public static void main(String[] args) throws IOException 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 IOException. c) The main method should simply terminate if the IOException occurs. d) The main method will not terminate if any exception occurs.c
- 1212) 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.b
- 1313) 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 static void main(String[] args) throws FileMissingException b) public static void main(String[] args) throws FileNotFoundException c) public static void main(String[] args) d) public static void main(String[] args) throws UnknownFileExceptionb
- 1414) 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 in = Scanner ("input.txt") b) Scanner in = new Scanner ("input.txt") c) Scanner in = Scanner.open(inputFile) d) Scanner in = new Scanner(inputFile)d
- 1515) 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.c
- 1621) 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.a
- 1729) 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 PrintWriter("c:/reports/dataOut.txt") b) new PrintWriter("c://reports//dataOut.txt") c) new PrintWriter("c:\reports\dataOut.txt") d) new PrintWriter("c:\\reports\\dataOut.txt")d
- 1838) Which of the following statements about reading and writing binary data is correct? a) You use the Scanner class to read and write binary data. b) You use the PrintWriter class to read and write binary data. c) You use the InputStream class to read binary data and the FileOutputStream class to write binary data. d) You use the InputStream class to write binary data and the FileOutputStream class to read binary data.c
- 1939) Insert the missing code in the following code fragment. This fragment is intended to read binary data. public static void main(String[] args) throws IOException { String address = "http://horstmann.com/bigjava.gif"; URL imageLocation = new URL(address); InputStream in = _________; . . . } a) new Scanner("http://horstmann.com/bigjava.gif") b) new Scanner(imageLocation) c) new imageLocation.openStream d) imageLocation.openStream()d
- 2041) When reading words with a Scanner object, a word is defined as ____. a) Any sequence of characters consisting of letters only. b) Any sequence of characters consisting of letters, numbers, and punctuation symbols only. c) Any sequence of characters consisting of letters and numbers only. d) Any sequence of characters that is not white space.d
- 2142) Consider the following code snippet. File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile); while (in.hasNext()) { String input = in.next(); } Which of the following statements about this code is correct? a) This code will read in a word at a time from the input file. b) This code will read in the entire input file in one operation. c) This code will read in a line at a time from the input file. d) This code will read in a character at a time from the input file.a
- 2243) When reading words using a Scanner object's next method, ____. a) any characters at the beginning of the input that are considered to be white space are consumed and become part of the word being read. b) any characters at the beginning of the input that are considered to be white space are consumed and do not become part of the word being read. c) the program must discard white space characters at the beginning of the input before calling the next method. d) any characters that are considered to be white space within the word become part of the word.b
- 2344) Insert the missing code in the following code fragment. This fragment is intended to read all words from a text file. File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(dataIn.txt); while (____________) { String input = in.next(); System.out.println(input); } a) in.getNext() b) in.peek() c) in.hasNext() d) in.nextWord()c
- 2445) 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.c
- 2546) Consider the following code snippet: Scanner in = new Scanner(. . .); in.useDelimiter("[^A-Za-z]+"); What characters will be ignored and not read in when 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.c
- 2647) 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.d
- 2748) 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.c
- 2849) 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()c
- 2950) 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("");d
- 3052) The ____ method of the Character class will indicate if a character contains white space. a) isValid() b) getChar() c) hasNext() d) isWhiteSpace()d
- 3153) 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.a
- 3254) Which String class method will remove spaces from the beginning and the end of a string? a) truncate() b) trim() c) clean() d) strip()b
- 3356) Consider the following code snippet: Scanner in = new Scanner(. . .); while (in.hasNextInt()) { . . . } Under which condition will the body of the while loop be processed? a) When an integer value is encountered in the input. b) When a non-integer value is encountered in the input. c) When any numeric value is encountered in the input. d) When any type of value is encountered in the input.a
- 3459) Consider the following code snippet: Scanner in = new Scanner(. . .); while (in.hasNextInt()) { . . . } Under which condition will the body of the while loop be processed? a) When an integer value is encountered in the input. b) When a non-integer value is encountered in the input. c) When an alphabetic value is encountered in the input. d) When any numeric value is encountered in the input.a
- 3562) Consider the following code snippet: Scanner in = new Scanner(. . .); String result = ""; int number = 0; if (in.hasNextInt()) { number = in.nextInt(); } result = in.next(); If the input begins with the characters 626.14 average, what values will number and result have after this code is executed? a) number will contain the value 626 and result will contain the value 14. b) number will contain the value 626.14 and result will contain the value average. c) number will contain the value 0 and result will contain the value 626.14. d) number will contain the value 0 and result will contain the value average.c
- 3663) Consider the following code snippet: if (in.hasNextDouble()) { number = in.nextDouble(); } result = in.next(); If the input contains the characters 626.14 average, what values will number and result have after this code is executed? a) number will contain the value 626 and result will contain the value 14. b) number will contain the value 626.14 and result will contain the value average. c) number will contain the value 0 and result will contain the value 626.14. d) number will contain the value 0 and result will contain the value average.b
- 3764) Consider the following code snippet: System.out.printf("%-12s%8.2f",description,totalPrice); Which of the following statements is correct? a) This code will produce 2 columns, with the description field left justified and the totalPrice field right justified. b) This code will produce 2 columns, with the description field right justified and the totalPrice field right justified. c) This code will produce 2 columns, with the description field right justified and the totalPrice field left justified. d) This code will produce 2 columns, with the description field left justified and the totalPrice field left justified.a
- 3867) Which of the following is the correct syntax for starting a Java program named myProg from a command line if the program requires two arguments named arg1 and arg2 to be supplied? a) java myProg arg1 arg2 b) java myProg(arg1, arg2) c) java myProg "arg1" "arg2" d) java myProg arg1, arg2a
- 3970) What is the purpose of the throw statement? a) It is used to pass arguments to another method. b) It is used to detect an error situation. c) It is used to pass control to an error handler when an error situation is detected. d) It is used to discard erroneous input.c
- 4074) Which of the following statements about exception reporting is true? a) Use the reportError statement to report that an exception has occurred. b) Use the reportException statement to report that an exception has occurred. c) Use the throwException statement to report that an exception has occurred. d) Use the throw statement to report that an exception has occurred.d
- 4175) 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.d
- 4276) 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.d
- 4378) 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 (IOException exception) b) catch (new exception (IOException)) c) catch (IllegalArgumentException exception) d) catch (IOException)a
- 4487) Which of the following code snippets about exceptions is correct? a) public void read(String filename) throws IOException, ClassNotFoundException b) public void read(String filename) throw IOException, ClassNotFoundException c) public void read(String filename) throw (IOException, ClassNotFoundException) d) public void read(String filename) throws IOException, throws ClassNotFoundExceptiona
- 4591) Consider the following code snippet: public double[] readInputFile(String filename) throws IOException { File inputFile = new File(filename); Scanner in = new Scanner(inputFile); try { readData(in); return data; } finally { inputFile.close(); } } Which of the following statements about this method's code is correct? a) The method will never execute the finally clause if an IOException exception occurs in this method. b) This method will pass any type of exception back to the caller. c) This method will pass any IOException-type exception back to the caller. d) Any exceptions that occur in this method will be suppressed.c
- 4693) Consider the following code snippet: try { File inputFile = new File(filename); Scanner in = new Scanner(inputFile); . . . } catch (Exception e) { } Which of the following statements about this code is correct? a) This code will not catch a FileNotFoundException that occurs in the try block. b) This code will pass any exceptions back to its caller. c) This code will catch exceptions that occur in the try block but will do nothing about the exceptions. d) This code will not catch any exceptions that occur in the try block.c
- 4794) See text document.c
- 4895) Consider the following code snippet: try { PrintWriter outputFile = new PrintWriter(filename); writeData(outputFile); } finally { outputFile.close(); } catch (IOException exception) { . . . } Which of the following statements about this code is correct? a) The file will be closed regardless of when an exception occurs. b) The file will be closed only if the PrintWriter constructor throws an exception. c) The file will be closed only if the writeData() statement throws an exception. d) It is not possible to determine whether the file will be closed if an exception occurs.a
- 4996) Consider the following code snippet: try { PrintWriter outFile = new PrintWriter(filename); writeData(outputFile); } catch (IOException exception) { . . . } finally { outputFile.close(); } What is wrong with this code? a) The program will attempt to close the file even if it has not been successfully opened. b) This code will not handle write errors. c) This code will ensure the data is written properly. d) There is nothing wrong with this code.a
- 5098) See text document.d
Information
- Description
- CSE 110 (Review) Flashcards on CSE 110 Chapter 07 Review, created by Branden Roper on 24/11/2017.
- Created by
- Branden Roper
- Views
- 2
- Tags
- Rating