Вы находитесь на странице: 1из 32

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 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.

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 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.

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) new PrintWriter(outputFileName)

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.

5) 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) If the output file cannot be opened or created due to a security error.

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.
b) If the input file does not exist

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?

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) File inFile = new File("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
a) Scanner

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.
c) The main method should simply terminate if the FileNotFoundException occurs.

11) 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) The main method should simply terminate if the IOException 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.
b) Data loss may occur if a program fails to close a PrintWriter object 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 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 UnknownFileException
b) public static void main(String[] args) throws FileNotFoundException

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 in = Scanner ("input.txt")


b) Scanner in = new Scanner ("input.txt")
c) Scanner in = Scanner.open(inputFile)
d) Scanner in = new Scanner(inputFile)
d) Scanner in = 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.
c) If a file named "output.txt" already exists, existing data will be deleted before data are added to the file.
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.
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.

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) 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.

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) imageLocation.openStream()

41) 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) Any sequence of characters that is not white space.

42) 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) 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.

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) in.hasNext()

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.
c) In Java, white space includes spaces, tab characters, and newline characters.

46) 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) Only non-alphabetic characters will be ignored.

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.
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.
c) Characters that are neither alphabetic nor numeric 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()
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()

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.

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:

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.

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.

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.

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.

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

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

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.

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.

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.

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)

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

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.
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.

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.

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.
a) The file will be closed regardless of when an exception occurs.

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.

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 exceptiond)
d) in.close()
d) in.close()

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:

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.

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()

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
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.

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

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()

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.

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.

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:

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.

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.

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.

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.

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

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

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

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

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.

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

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

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.

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.

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.

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.
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()

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)

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 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.
81) The Java compiler requires that your program handle which type of exceptions?

a) Checked.
b) Fatal.
c) Unchecked.
d) Severe.
a) Checked.

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

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:

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.

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

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

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.

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

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.

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.

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

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.

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

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

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.

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.

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()

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

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.

Вам также может понравиться