chapter 7 part2
81) The Java compiler requires that your program handle which type of exceptions? a) Checked. b) Fatal. c) Unchecked. d) Severe.
a) Checked.
86) An example of a fatal error that rarely occurs and is beyond your control is the ____. a) OutOfMemoryError b) RuntimeException c) FileNotFoundException d) NumberFormatException
a) OutOfMemoryError
96) 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) The program will attempt to close the file even if it has not been successfully opened.
79) Which method of an exception object will retrieve a description of the exception that occurred? a) printStackTrace() b) printMessage() c) getMessage() d) getDescription()
c) getMessage()
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.
a) The throws clause must list all checked exceptions that this method may throw, and may also list unchecked exceptions.
72) Consider the following code snippet: throw new IllegalArgumentException("This operation is not allowed!"); Which of the following statements about this code is correct? a) This code constructs an object of type IllegalArgumentException and throws the object. b) This code throws an existing IllegalArgumentException object. c) This code constructs an object of type IllegalArgumentException and reserves it for future use. d) This code will not compile
a) This code constructs an object of type
64) 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) This code will produce 2 columns, with the description field left justified and the totalPrice field right justified.
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.
a) This code will read in an entire line from the file in each iteration of the loop.
56) 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) When an integer value is encountered in the input.
59) 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) When an integer value is encountered in the input.
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 (IOException exception) b) catch (new exception (IOException)) c) catch (IllegalArgumentException exception) d) catch (IOException)
a) catch (IOException exception)
67) 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, arg2
a) java myProg arg1 arg2
87) 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 ClassNotFoundException
a) public void read(String filename) throws IOException, ClassNotFoundException
51) The Scanner class's ____ method is used to specify a pattern for word boundaries when reading text. a) useDelimiter() b) usePattern() c) setDelimiter() d) setPattern()
a) useDelimiter()
88) When a program throws an exception within a method that has no try-catch block, which of the following statements about exception handling is true? a) Execution will continue with the next statement in the method. b) The current method terminates immediately. c) The current method must decide whether to continue or terminate. d) The user must decide whether to continue or terminate the program
b) The current method terminates immediately.
85) Consider the following code snippet: public void readFile(String filename) throws FileNotFoundException { File inFile = new File(filename); Scanner in = new Scanner(inFile); . . . } If the file cannot be located, which of the following statements about this code is correct? a) This method must handle the exception in the body of the method. b) This method will be terminated if the file cannot be located. c) This method must use a throw statement to pass the error back to its caller. d) It cannot be determined how the method must handle the exception if the file cannot be located.
b) This method will be terminated if the file cannot be located.
63) 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) number will contain the value 626.14 and result will contain the value average.
54) 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) trim()
90) Which of the following statements about the finally clause in a try block is NOT true? a) The finally clause will be executed after the last statement of the try block completes without exception. b) The finally clause will be executed after the last statement of a catch clause completes if this try block catches an exception. c) If no exception occurs, the finally clause will not be executed. d) The finally clause will be executed when an exception is thrown in the try block but not caught.
c) If no exception occurs, the finally clause will not be executed.
73) In the hierarchy of Exception classes, the NumberFormatException class is a subclass of the ____ class. a) ArithmeticException. b) ClassCastException. c) IllegalArgumentException. d) IllegalStateException
c) IllegalArgumentException.
70) 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) It is used to pass control to an error handler when an error situation is detected.
65) Consider the following code snippet: Scanner in = new Scanner(. . .); in.useDelimiter("[A-Za-z]+"); What characters will be read in using this code? a) Only alphabetic characters will be read in. b) Only numeric characters will be read in. c) Only non-alphabetic characters will be read in. d) Only non-numeric characters will be read in.
c) Only non-alphabetic characters will be read in.
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.
c) The compiler ensures that the program is handling checked exceptions.
94) Consider the following code snippet: try { PrintWriter outputFile = new PrintWriter(filename); try { 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 the exception occurs. b) The file will be closed if the PrintWriter constructor throws an exception. c) The file will be closed if the writeData() statement throws an exception. d) It is not possible to determine whether the file will be closed if an exception occurs.
c) The file will be closed if the writeData() statement throws an exception
93) 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) This code will catch exceptions that occur in the try block but will do nothing about the exceptions.
55) Consider the following code snippet. Scanner in = new Scanner(. . .); while (in.hasNextLine()) { String input = in.nextLine(); } 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.
c) This code will read in a line at a time from the input file
57) Consider the following code snippet. Scanner in = new Scanner(. . .); while (in.hasNextDouble()) { double input = in.nextDouble(); } Which of the following statements about this code is correct? a) This code will read in one 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 one floating point value at a time from the input file. d) This code will read in one character at a time from the input file.
c) This code will read in one floating point value at a time from the input file
91) 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) This method will pass any IOException-type exception back to the caller.
82) Which of the following statements about checked and unchecked exceptions is NOT true? a) Handling of checked exceptions is enforced by the compiler. b) Unchecked exceptions are the programmer's fault. c) Unchecked exceptions belonging to subclasses of the RunTimeException class are beyond the control of the programmer. d) Internal errors belonging to subclasses of the Error class are beyond the control of the programmer.
c) Unchecked exceptions belonging to subclasses of the RunTimeException class are beyond the control of the programmer
68) When you start a Java program from a command line and supply argument values, the values ____. a) are stored as int values b) are stored as float values c) are stored as String values d) are stored as the type of value indicated by the argument
c) are stored as String values
58) Insert the missing code in the following code fragment. This fragment is intended to read floating-point numbers from a text file. Scanner in = new Scanner(. . .); while (____________) { double hoursWorked = in.nextDouble(); System.out.println(hoursWorked); } a) in.getNextDouble() b) in.peek() c) in.hasNextDouble() d) in.readNextWord()
c) in.hasNextDouble()
62) 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) number will contain the value 0 and result will contain the value 626.14.
77) Which method of an exception object will provide information about the chain of method calls that led to the exception? a) printCallStack() b) getCallStack() c) printStackTrace() d) getStackTrace()
c) printStackTrace()
60) Consider the following code snippet: Scanner in = new Scanner(. . .); int ageValue = in.nextInt(); If there is no integer number appearing next in the input, what will occur? a) ageValue will contain a null value. b) ageValue will contain a zero. c) ageValue will contain whatever characters are encountered. d) An exception will occur.
d) An exception will occur.
66) Which of the following statements about command line arguments is correct? a) Command line arguments must be read with a Scanner object. b) You must declare additional parameters in the main method to receive command line argument values. c) You cannot pass arguments to a program when starting the program from a command line prompt. d) Command line arguments can be read using the main method's args parameter.
d) Command line arguments can be read using the main method's args parameter
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.
d) If an exception has no handler, the program will be terminated.
95) 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.
d) It is not possible to determine whether the file will be closed if an exception occurs
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.
d) Statements that may cause an exception should be placed within a try block.
97) Consider the following code snippet written in Java 7: try (PrintWriter outputFile = new PrintWriter(filename)) { writeData(outputFile); } Which of the following statements about this Java 7 code is correct? a) The program will terminate with an unhandled exception if the PrintWriter constructor fails. b) The close method of the outputFile object will be automatically invoked when the try block ends, but only if no exception occurred. c) The close method of the outputFile object will be automatically invoked when the try block ends, but only if an exception occurs. d) The close method of the outputFile object will be automatically invoked when the try block ends, whether or not an exception has occurred.
d) The close method of the outputFile object will be automatically invoked when the try block ends, whether or not an exception has occurred.
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
d) The program should place the outputFile.close() statement within a finally clause of a try block to ensure that the file is closed
100) Consider the following code snippet: Scanner in = new Scanner(. . .); . . . if (in.hasNext()) { throw new IOException("End of file expected"); } Which of the following statements about this code is correct? a) The program will display the message "End of file expected" if there is no data. b) The program will throw an exception if there is no data. c) The program will display the message "End of file expected" if there is data left in the input when the if statement is executed. d) The program will throw an exception if there is data left in the input when the if statement is executed.
d) The program will throw an exception if there is data left in the input when the if statement is executed.
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.
d) The throws clause should list the names of all exceptions that the method will not handle.
71) Consider the following code snippet: throw IllegalStateException("This operation is not allowed!"); Which of the following statements about this code is correct? a) This code constructs an object of type IllegalArgumentException and throws the object. b) This code throws an existing IllegalArgumentException object. c) This code constructs an object of type IllegalArgumentException and reserves it for future use. d) This code will not compile
d) This code will not compile
92) Which of the following statements about exception handling is recommended by the textbook? a) All exceptions should be handled where they are first detected. b) All exceptions should be handled at the top of the chain of methods. c) Throw an exception only when the problem can be handled. d) Throw an exception as soon as a problem is detected, but only catch exceptions when the problem can be handled
d) Throw an exception as soon as a problem is detected, but only catch exceptions when the problem can be handled
74) 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) Use the throw statement to report that an exception has occurred.
69) You have opened a command prompt window and you have entered the following: java myProg Bob Smith Which of the following statements is correct? a) You have supplied one argument value, and this value can be accessed in the main method using the arg1 parameter. b) You have supplied two argument values, and these values can be accessed in the main method using the arg1 and arg2 parameters. c) You have supplied one argument value, and this value can be accessed in the main method using the args parameter. d) You have supplied two argument values, and these values can be accessed in the main method using the args parameter
d) You have supplied two argument values, and these values can be accessed in the main method using the args parameter
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()
d) hasNextInt()
98) 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. public void String readFile() throws IOException { File inputFile = new File(. . .); Scanner in = new Scanner(inputFile); try { while (in.hasNext()) { . . . } } finally { ___________________ } } a) catch (IOException exception) b) catch (FileNotFound exception) c) catch (IllegalArgumentException exception) d) in.close()
d) in.close()
52) The ____ method of the Character class will indicate if a character contains white space. a) isValid() b) getChar() c) hasNext() d) isWhiteSpace()
d) isWhiteSpace()
99) 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. public void String readFile() _________________ { File inputFile = new File(. . .); Scanner in = new Scanner(inputFile); try { while (in.hasNext()) { . . . } } finally { in.close(); } } a) throws IOException exception b) throws IOException, FileNotFound c) throws IllegalArgumentException d) throws IOException
d) throws IOException