Академический Документы
Профессиональный Документы
Культура Документы
a) new Scanner(inputFileName)
b) new Scanner(outputFileName)
c) new Scanner(inputFile)
d) new Scanner(System.in)
c) new Scanner(inputFile)
2) Insert the missing code in the following code fragment. This fragment is intended to read an input file.
a) extends FileNotFoundException
b) throws FileNotFoundException
c) inherits FileNotFoundException
d) catches FileNotFoundException
b) throws FileNotFoundException
3) Insert the missing code in the following code fragment. This fragment is intended to read a file and
write to a file.
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.
c) If the output file already exists, the existing data will be discarded before new data are written into the
file.
7) 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) Both the input and the output file should be explicitly closed in the program.
8) Which of the following is the correct syntax for creating a File object?
9) Which of the following objects should be used for reading from a text file?
a) Scanner
b) ReadStream
c) PrintStream
d) ReadFile
a) Scanner
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) The main method should simply terminate if the FileNotFoundException occurs.
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) The main method should simply terminate if the IOException occurs.
12) Which of the following statements about a PrintWriter object is true?
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?
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) 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) This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value.
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.
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) new PrintWriter("c:\\reports\\dataOut.txt")
38) 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) You use the InputStream class to read binary data and the FileOutputStream class to write binary data
39) Insert the missing code in the following code fragment. This fragment is intended to read binary data.
41) When reading words with a Scanner object, a word is defined as ____.
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) This code will read in a word at a time from the input file.
43) 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) 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.
44) Insert the missing code in the following code fragment. This fragment is intended to read all words
from a text file.
a) in.getNext()
b) in.peek()
c) in.hasNext()
d) in.nextWord()
c) in.hasNext()
45) Which of the following statements about white space in Java is correct?
What characters will be ignored and not read in when using this code?
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) Only non-numeric characters will be ignored.
What characters will be ignored and not read in using this code?
49) Insert the missing code in the following code fragment. This fragment is intended to read characters
from a text file.
while (in.hasNext())
{
char ch = ____________;
System.out.println(ch);
}
a) in.getNext()
b) in.next()
c) in.next.charAt(0)
d) in.nextChar()
c) in.next.charAt(0)
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("")
d) Scanner in = new Scanner(. . .);
in.useDelimiter("")
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()
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.
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()
56) Consider the following code snippet:
Under which condition will the body of the while loop be processed?
Under which condition will the body of the while loop be processed?
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.
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.
System.out.printf("%-12s%8.2f",description,totalPrice);
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.
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) 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.
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) 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.
93) Consider the following code snippet:
try
{
File inputFile = new File(filename);
Scanner in = new Scanner(inputFile);
...
}
catch (Exception e)
{
}
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.
try
{
PrintWriter outputFile = new PrintWriter(filename);
try
{
writeData(outputFile);
}
finally
{
outputFile.close();
}
}
catch (IOException exception)
{
...
}
try
{
PrintWriter outputFile = new PrintWriter(filename);
writeData(outputFile);
}
finally
{
outputFile.close();
}
catch (IOException exception)
{
...
}
try
{
PrintWriter outFile = new PrintWriter(filename);
writeData(outputFile);
}
catch (IOException exception)
{
...
}
finally
{
outputFile.close();
}
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.
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.
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()
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()
53) Consider the following code snippet:
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.
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()
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
56) Consider the following code snippet:
Under which condition will the body of the while loop be processed?
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
58) Insert the missing code in the following code fragment. This fragment is intended to read floating-point
numbers from a text file.
a) in.getNextDouble()
b) in.peek()
c) in.hasNextDouble()
d) in.readNextWord()
c) in.hasNextDouble()
Under which condition will the body of the while loop be processed?
If there is no integer number appearing next in the input, what will occur?
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()
62) Consider the following code snippet:
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.
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.
System.out.printf("%-12s%8.2f",description,totalPrice);
66) Which of the following statements about command line arguments is correct?
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?
68) When you start a Java program from a command line and supply argument values, the values ____.
69) You have opened a command prompt window and you have entered the following:
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
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.
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.
a) printCallStack()
b) getCallStack()
c) printStackTrace()
d) getStackTrace()
c) printStackTrace()
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();
}
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()
80) Which of the following statements about checked and unchecked exceptions is true?
a) Checked.
b) Fatal.
c) Unchecked.
d) Severe.
a) Checked.
82) Which of the following statements about checked and unchecked exceptions is NOT true?
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.
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.
85) Consider the following code snippet:
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.
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
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?
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
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.
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.
92) Which of the following statements about exception handling is recommended by the textbook?
try
{
File inputFile = new File(filename);
Scanner in = new Scanner(inputFile);
...
}
catch (Exception e)
{
}
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.
try
{
PrintWriter outputFile = new PrintWriter(filename);
try
{
writeData(outputFile);
}
finally
{
outputFile.close();
}
}
catch (IOException exception)
{
...
}
try
{
PrintWriter outputFile = new PrintWriter(filename);
writeData(outputFile);
}
finally
{
outputFile.close();
}
catch (IOException exception)
{
...
}
try
{
PrintWriter outFile = new PrintWriter(filename);
writeData(outputFile);
}
catch (IOException exception)
{
...
}
finally
{
outputFile.close();
}
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.
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.
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.
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.
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.