Full Test Bank Input/Output and Exception Handling Ch.11 - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.

Full Test Bank Input/Output and Exception Handling Ch.11

Chapter Number: 11 Input/Output and Exception Handling

Question type: Multiple Choice

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)

Title: Complete code for reading an input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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

Title: Complete code for reading an input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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)

Title: Complete code for writing an output file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Which statement about PrintWriter is correct?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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.

Title: When does PrintWriter generate a FileNotFoundException?

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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.

Title: When does Scanner generate a FileNotFoundException?

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Which statement about closing files is correct?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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

Title: What is the correct syntax for creating a File object?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

9. Which of the following objects should be used for reading from a text file?

a) Scanner

b) ReadStream

c) PrintStream

d) ReadFile

Title: Which object should be used for reading from a text file?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Which statement about exceptions is correct?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Which statement about exceptions is correct?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Which statement about a PrintWriter is true?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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

Title: Which is the correct code for handling a missing input file?

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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)

Title: What is the correct syntax for opening a file with a Scanner object?

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Which statement about the PrintWriter object is correct?

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

16. The PrintWriter class is an enhancement of the ____ class.

a) Scanner

b) ReadStream

c) PrintStream

d) File

Title: The PrintWriter class is an enhancement of the ____ class.

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

17. Which of the following statements about reading and writing text files is correct?

a) You use the Scanner class to read and write text files.

b) You use the PrintWriter class to read and write text files.

c) You use the Scanner class to read text files and the PrintWriter class to write text files.

d) You use the Scanner class to write text files and the PrintWriter class to read text files.

Title: Which statement about reading and writing text files is correct?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

18. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt and write to an output file named dataOut.txt.

public static void main(String[] args) throws FileNotFoundException

{

String inputFileName = "dataIn.txt";

String outputFileName = "dataOut.txt";

File inputFile = _________________;

Scanner in = new Scanner(inputFile)

. . .

}

a) new File(inputFileName)

b) new File(outputFileName)

c) new File(inputFile)

d) new File(System.in)

Title: Complete code for reading an input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

19. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt and write to an output file named dataOut.txt.

public static void main(String[] args) throws FileNotFoundException

{

String inputFileName = "dataIn.txt";

String outputFileName = "dataOut.txt";

File inputFile = new File(inputFileName);

Scanner in = _________;

. . .

}

a) new File(inputFileName)

b) new File("dataIn.txt")

c) new Scanner(inputFile)

d) new Scanner("dataIn.txt")

Title: Complete code for reading an input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

20. Consider the following code snippet.

File hoursFile = new File("hoursWorked.txt");

Your program must read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this?

a) Scanner in = new Scanner("hoursWorked.txt");

b) Scanner in = Scanner("hoursWorked.txt");

c) Scanner in = new Scanner(hoursFile);

d) Scanner in = Scanner.open(hoursFile);

Title: Using a Scanner to read a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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.

Title: Using a Scanner to read a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

22. Consider the following code snippet.

Scanner inputFile = new Scanner("hoursWorked.txt");

Which of the following statements is correct?

a) This code will treat the string "hoursWorked.txt" as an input value.

b) This code will create a new file named "hoursWorked.txt".

c) This code will open an existing file named "hoursWorked.txt" for reading.

d) This code will open a file named "hoursWorked.txt" for writing.

Title: Using a Scanner to read a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

23. Consider the following code snippet.

PrintWriter outFile = new PrintWriter("dataOut.txt");

Which of the following statements about the PrintWriter object is correct?

a) If a file named "dataOut.txt" already exists, an exception will occur.

b) If a file named "dataOut.txt" already exists, existing data will be deleted before new data is added to the file.

c) If a file named "dataOut.txt" already exists, new data will be added to the end of the file.

d) If a file named "dataOut.txt" already exists, a new file named "dataOut_1.txt" will be created and used.

Title: Using a PrintWriter object to write a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

24. Consider the following code snippet.

PrintWriter outputFile = new PrintWriter("payrollReport.txt");

Which of the following statements about the PrintWriter object is correct?

a) If a file named "payrollReport.txt" already exists, an exception will occur.

b) If a file named "payrollReport.txt" already exists, existing data will be deleted before new data is added to the file.

c) If a file named "payrollReport.txt" already exists, new data will be added to the end of the file.

d) If a file named "payrollReport.txt" already exists, a new file named "payrollReport_1.txt" will be created and used.

Title: Using a PrintWriter object to write a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

25. Which of the following statements about using a PrintWriter object is NOT 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) PrintWriter is an enhancement of the PrintStream class.

d) A program can write to a PrintWriter using println.

Title: Using a PrintWriter object to write a file

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

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

a) public 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

Title: Handling a missing input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

27. Your program must read in an existing text file. You want the program to terminate if any exception related to the file occurs. Which of the following indicates the correct code for the main method?

a) public static void main(String[] args) throws IOException

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

Title: Handling file exceptions

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

28. Insert the missing code in the following code fragment. This fragment is intended to read an input file named hoursWorked.txt. You want the program to terminate if the file does not exist.

public static void main(String[] args) ______________

{

File inputFile = new File("hoursWorked.txt");

Scanner in = new Scanner(inputFile);

. . .

}

a) catch FileMissingException

b) catch FileNotFoundException

c) throws FileMissingException

d) throws FileNotFoundException

Title: Handling a missing input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

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

Title: Using a PrintWriter object to write a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

30. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt that resides in a folder named payroll on the C: drive of a Windows system.

public static void main(String[] args) throws FileNotFoundException

{

File inputFile = ________;

Scanner in = new Scanner(inputFile);

. . .

}

a) new File(c:/payroll/"dataIn.txt")

b) new File(c://payroll//"dataIn.txt")

c) new File("c:\payroll\dataIn.txt")

d) new File("c:\\payroll\\dataIn.txt")

Title: Complete code for reading an input file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Common Error 11.1 Backslashes in File Names

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

a) inputFile = new File("c:\java\myProg\input.txt");

b) inputFile = new File.open("c:\java\myProg\input.txt");

c) inputFile = new File("c:\\java\\myProg\\input.txt");

d) inputFile = new File.open("c:\\java\\myProg\\input.txt");

Title: Which is the correct code for opening an input file?

Difficulty: Hard

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Common Error 11.1 Backslashes in File Names

32. Insert the missing code in the following code fragment. This fragment is intended to read a web page.

public static void main(String[] args) throws IOException

{

String address = "http://horstmann.com/index.html";

URL pageLocation = new URL(address);

Scanner in = _________;

. . .

}

a) new Scanner("http://horstmann.com/index.html")

b) new Scanner(pageLocation)

c) new Scanner(new File(pageLocation))

d) new Scanner(pageLocation.openStream())

Title: Complete code for reading a web page

Difficulty: Hard

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.1 Reading web pages

33. Which of the following statements about reading web pages is true?

a) A Scanner object cannot be used to read a web page.

b) You must create a File object to use with a Scanner object to read a web page.

c) You must create a URL object to use with a Scanner object to read a web page.

d) You must use the openStream() method of a Scanner object to read a web page.

Title: Which statement about reading a web page is correct?

Difficulty: Hard

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.1 Reading web pages

34. Which Java class implements a file dialog box for selecting a file by a program with a graphical user interface?

a) JOptionPane

b) JFileOption

c) JFilePane

d) JFileChooser

Title: Which Java class is used for selecting files?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.1 Reading web pages

35. Which method of the JFileChooser object will return the file object that describes the file chosen by the user at runtime?

a) getFile()

b) getSelectedFilePath()

c) getSelectedFile()

d) getFilePath()

Title: Using JFileChooser to select a file

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.1 Reading web pages

36. Which return value of the JFileChooser object's showOpenDialog method indicates that a file was chosen by the user at run time?

a) APPROVE_OPTION

b) CANCEL_OPTION

c) OK_OPTION

d) SELECTED_OPTION

Title: Using JFileChooser to select a file

Difficulty: Medium

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.1 Reading web pages

37. Insert the missing code in the following code fragment. This fragment is intended to allow the user to select a file to be opened.

JFileChooser chooser = new JFileChooser();

Scanner in = null;

if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)

{

File selectedFile = __________;

in = new Scanner(selectedFile);

. . .

}

a) chooser.getFileName()

b) chooser.getSelectedFileName()

c) chooser.getFilePath()

d) chooser.getSelectedFile()

Title: Complete code for selecting a file with JFileChooser

Difficulty: Hard

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.1 Reading web pages

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.

Title: Which statement about reading and writing binary data is correct?

Difficulty: Easy

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.3 Reading and writing 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()

Title: Complete code for reading binary data

Difficulty: Hard

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.3 Reading and writing binary data

40. 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 = _________;

InputStream in = imageLocation.openStream());

. . .

}

a) new Scanner("http://horstmann.com/bigjava.gif")

b) new Scanner(address)

c) new imageLocation.openStream

d) new URL(address)

Title: Complete code for reading binary data

Difficulty: Hard

Section Reference 1: 11.1 Reading and writing text files

Section Reference 2: Special Topic 11.3 Reading and writing binary data

42. Select an expression to complete the following statement, which is designed to construct a PrintWriter object to write the program output to a file named dataout.txt

PrintWriter theFile = _______________________;

a) new PrintStream("dataout.txt")

b) new File("dataout.txt")

c) new PrintWriter("dataout.txt")

d) new Scanner("dataout.txt")

Title: Select correct expression to construct PrintWriter object

Difficulty: Easy

Section Reference 1: 11.1 Reading and Writing Text Files

43. Assuming that inputFile is a Scanner object used to read words from a text file, select an expression to complete the following code segment, which counts the number of words in the input file.

int count = 0;

while (inputFile.hasNext())

{

String word = _______________;

count++;

}

System.out.println(count);

a) inputFile.next()

b) inputFile.nextInt()

c) inputFile.nextDouble()

d) inputFile.nextString()

Title: Select correct expression to input String objects from a text file

Difficulty: Medium

Section Reference 1: 11.1 Reading and Writing Text Files

44. Assume that inputFile is a Scanner object used to read data from a text file which contains a number of lines. Some lines contain an alphabetic string, while others contain a single integer value. Select an expression to complete the following code segment, which counts the number of integer values in the input file.

int count = 0;

while (inputFile.hasNextLine())

{

if (________________________________)

{

count++;

}

inputFile.nextLine();

}

System.out.println(count);

a) inputFile.hasNext()

b) inputFile.hasNextInt()

c) inputFile.nextInt()

d) Character.isDigit(inputFile.next())

Title: Select correct expression to test for integer values in a text file

Difficulty: Medium

Section Reference 1: 11.2 Text Input and Output

45. Assume that inputFile is a Scanner object used to read data from a text file which contains a number of lines. Each line contains an arbitrary number of words, with at least one word per line. Select an expression to complete the following code segment, which prints the last word in each line.

while (inputFile.hasNextLine())

{

String word = "";

String line = inputFile.nextLine();

Scanner words = new Scanner(line);

while (_________________)

{

word = words.next();

}

System.out.println(word);

}

a) words.hasNext()

b) words.hasNextInt()

c) line.hasNext()

d) inputFile.hasNext()

Title: Select correct expression to read words from a line

Difficulty: Medium

Section Reference 1: 11.2 Text Input and Output

46. Assume that inputFile is a Scanner object used to read data from a text file which contains a series of double values. Select an expression to complete the following code segment, which reads the values and prints them in standard output, one per line, in a field 15 characters wide, with two digits after the decimal point.

while (inputFile.hasNextDouble())

{

double value = inputFile.nextDouble();

___________________________________ // statement do display double value

}

a) System.out.printf("%15f",value);

b) System.out.printf("%.2f",value);

c) System.out.printf("%15.2f",value);

d) System.out.printf("%15.2f\n",value);

Title: Identify proper arguments to use with printf statement

Difficulty: Medium

Section Reference 1: 11.2 Text Input and Output

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

Title: Which statement about reading and writing text files is correct?

Difficulty: Easy

Section Reference 1: 11.2 Text input and output

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

Title: Using Scanner's next method

Difficulty: Medium

Section Reference 1: 11.2 Text input and output

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

Title: Using the Scanner's next method to read words

Difficulty: Hard

Section Reference 1: 11.2.1 Reading words

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

Title: Complete code for reading words from a text file

Difficulty: Easy

Section Reference 1: 11.2.1 Reading words

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

Title: What is white space?

Difficulty: Medium

Section Reference 1: 11.2.1 Reading words

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

Title: Using delimiters with the Scanner

Difficulty: Medium

Section Reference 1: 11.2.1 Reading words

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

Title: Using delimiters with the Scanner

Difficulty: Medium

Section Reference 1: 11.2.1 Reading words

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

Title: Using delimiters with the Scanner

Difficulty: Hard

Section Reference 1: 11.2.1 Reading words

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

Title: Complete code for reading characters from a text file

Difficulty: Medium

Section Reference 1: 11.2.2 Reading characters

56. 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("");

Title: Using the Scanner's next method to read characters

Difficulty: Medium

Section Reference 1: 11.2.2 Reading characters

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

Title: Using the Scanner to read text

Difficulty: Easy

Section Reference 1: 11.2.2 Reading characters

58. The ____ method of the Character class will indicate if a character contains white space.

a) isValid()

b) getChar()

c) hasNext()

d) isWhiteSpace()

Title: Character testing methods

Difficulty: Easy

Section Reference 1: 11.2.3 Classifying characters

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

Title: Using the Scanner to read text files

Difficulty: Easy

Section Reference 1: 11.2.4 Reading lines

60. Which String class method will remove spaces from the beginning and the end of a string?

a) truncate()

b) trim()

c) clean()

d) strip()

Title: How to remove spaces from a string

Difficulty: Easy

Section Reference 1: 11.2.4 Reading lines

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

Title: Using Scanner's nextLine() method

Difficulty: Easy

Section Reference 1: 11.2.4 Reading Lines

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

Title: Using the Scanner to read text files

Difficulty: Hard

Section Reference 1: 11.2.7 Avoiding errors when reading numbers

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

Title: Using Scanner's nextDouble method

Difficulty: Medium

Section Reference 1: 11.2.7 Avoiding Errors When Reading Numbers

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

Title: Complete code for reading words from a text file

Difficulty: Easy

Section Reference 1: 11.2.7 Avoiding Errors when Reading Numbers

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

Title: Using the Scanner to read text files

Difficulty: Hard

Section Reference 1: 11.2.7 Avoiding errors when reading numbers

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

Title: Using the Scanner to read numeric values

Difficulty: Hard

Section Reference 1: 11.2.7 Avoiding errors when reading numbers

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

Title: Using the Scanner to read numeric values

Difficulty: Easy

Section Reference 1: 11.2.7 Avoiding errors when reading numbers

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

Title: Reading mixed number, word and line input

Difficulty: Medium

Section Reference 1: 11.2.8 Mixing Number, Word, and Line Input

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

Title: Reading mixed number, word and line input

Difficulty: Medium

Section Reference 1: 11.2.8 Mixing Number, Word, and Line Input

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

Title: Formatting output

Difficulty: Medium

Section Reference 1: 11.2 Text Input and Output 

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

Title: Using delimiters with the Scanner

Difficulty: Medium

Section Reference 1: 11.2 Text Input and Output 

Section Reference 2: Special Topic 11.4 Regular expressions

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

Title: How are command line arguments handled?

Difficulty: Medium

Section Reference 1: 11.3 Command Line Arguments

73 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

Title: How are command line arguments entered?

Difficulty: Medium

Section Reference 1: 11.3 Command Line Arguments

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

Title: How are command line arguments stored?

Difficulty: Medium

Section Reference 1: 11.3 Command Line Arguments

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

Title: Which statement about supplying arguments at runtime is correct?

Difficulty: Hard

Section Reference 1: 11.3 Command Line Arguments

76. Select an expression to complete the program segment below, which is designed to read data from a file whose name is specified as the first command line argument. If no command line arguments are given, the program reads data from the default.txt file.

String fileName = "default.txt";

if ( ________________________ )

{

fileName = args[0];

}

// additional statements to open file and read data

a) args > 0

b) args[0] != null

c) args.length >= 0

d) args.length > 0

Title: Identify expression to open file using command line argument

Difficulty: Medium

Section Reference 1: 11.3 Command Line Arguments

77. Complete the code fragment below, which is designed to throw an exception if String variable accountNumber has more than seven characters.

if (accountNumber.length() > 7)

{

___________________________________________

}

a) throw IllegalArgumentException("Account number exceeds maximum length");

b) throw new IllegalArgumentException("Account number exceeds maximum length");

c) throws IllegalArgumentException("Account number exceeds maximum length");

d) throws new IllegalArgumentException("Account number exceeds maximum length");

Title: Complete code segment to throw an exception

Difficulty: Easy

Section Reference 1: 11.4 Exception Handling

78. Select an expression to complete the program segment below, which displays an error message and terminates normally if String variable accountNumber does not contain an integer value.

try

{

int number = Integer.parseInt(accountNumber);

}

catch ( ________________________ )

{

System.out.println("Account number is not an integer value");

}

a) IOException exception

b) ArithmeticException exception

c) NumberFormatException exception

d) IllegalArgumentException exception

Title: Identify correct expression to use in catch clause

Difficulty: Medium

Section Reference 1: 11.4 Exception Handling

79. Select an appropriate expression to complete the header for the method below.

public void openFile(String inputFile) ______________________________

{

File theFile = new File(inputFile);

Scanner data = new Scanner(theFile);

// additional statements to input data from file

}

a) throw FileNotFoundException

b) throw new FileNotFoundException

c) throws FileNotFoundException

d) throws new FileNotFoundException

Title: Complete header for a method involving a checked exception

Difficulty: Easy

Section Reference 1: 11.4 Exception Handling

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

Title: What is the purpose of the throw statement?

Difficulty: Easy

Section Reference 1: 11.4.1 Throwing Exceptions

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

Title: Which statement about the IllegalStateException object is correct?

Difficulty: Medium

Section Reference 1: 11.4.1 Throwing Exceptions

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

Title: Which statement about the IllegalStateException object is correct?

Difficulty: Medium

Section Reference 1: 11.4.1 Throwing Exceptions

83. In the hierarchy of Exception classes, the NumberFormatException class is a subclass of the ____ class.

a) ArithmeticException.

b) ClassCastException.

c) IllegalArgumentException.

d) IllegalStateException.

Title: The NumberFormatException class is a subclass of the ____ class.

Difficulty: Medium

Section Reference 1: 11.4.1 Throwing Exceptions

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

Title: Which statement about exception reporting is true?.

Difficulty: Easy

Section Reference 1: 11.4.1 Throwing Exceptions

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

Title: Which statement about exception handing is true?

Difficulty: Easy

Section Reference 1: 11.4.2 Catching Exceptions

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

Title: Which statement about exceptions is true?

Difficulty: Easy

Section Reference 1: 11.4.2 Catching Exceptions

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

Title: Which method gives information about the exception?

Difficulty: Easy

Section Reference 1: 11.4.2 Catching Exceptions

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

Title: Complete the code about the exception handling

Difficulty: Easy

Section Reference 1: 11.4.2 Catching Exceptions

89. Which method of an exception object will retrieve a description of the exception that occurred?

a) printStackTrace()

b) printMessage()

c) getMessage()

d) getDescription()

Title: Which method gives a description of an exception?

Difficulty: Medium

Section Reference 1: 11.4.2 Catching Exceptions

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

Title: Which statement about checked and unchecked exceptions is true?

Difficulty: Medium

Section Reference 1: 11.4.3 Checked Exceptions

91. The Java compiler requires that your program handle which type of exceptions?

a) Checked.

b) Fatal.

c) Unchecked.

d) Severe.

Title: The Java compiler requires the program to handle what type of exceptions?

Difficulty: Easy

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: Which statement about checked and unchecked exceptions is NOT true?

Difficulty: Medium

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: If a method cannot handle an exception, what should be done?

Difficulty: Hard

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: Which statement about the throws clause is correct?

Difficulty: Hard

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: Which statement about the throws clause is correct?

Difficulty: Hard

Section Reference 1: 11.4.3 Checked Exceptions

96. An example of a fatal error that rarely occurs and is beyond your control is the ____.

a) OutOfMemoryError

b) RuntimeException

c) FileNotFoundException

d) NumberFormatException

Title: An example of a rare fatal error is ___.

Difficulty: Easy

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: Which code snippet about exceptions is correct?

Difficulty: Easy

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: Which statement about exception handling is true?

Difficulty: Medium

Section Reference 1: 11.4.3 Checked Exceptions

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

Title: Which statement about handling file exceptions is correct?

Difficulty: Hard

Section Reference 1: 11.4.4 The finally Clause

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

Title: Which statement about the finally clause is NOT true?

Difficulty: Hard

Section Reference 1: 11.4.4 The finally Clause

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

Title: Which statement about exceptions encountered in this code is correct?

Difficulty: Hard

Section Reference 1: 11.4.4 The finally Clause

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

Title: Which statement about exception handling is recommended?

Difficulty: Medium

Section Reference 1: Programming Tip 11.1 Throw Early, Catch Late

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

Title: Which statement about exception handling is correct?

Difficulty: Medium

Section Reference 1: Programming Tip 11.2 Do Not Squelch Exceptions

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

Title: Which statement about this code is correct?

Difficulty: Hard

Section Reference 1: Programming Tip 11.3 Do Not Use catch and finally in the Same try Statement

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

Title: Which statement about exceptions in this code is correct?

Difficulty: Hard

Section Reference 1: Programming Tip 11.3 Do Not Use catch and finally in the Same try Statement

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

Title: What is wrong with this code about exception handling?

Difficulty: Hard

Section Reference 1: Programming Tip 11.3 Do Not Use catch and finally in the Same try Statement

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

Title: Which statement about this Java 7 code is correct?

Difficulty: Hard

Section Reference 1: Special Topic 11.5 Automatic Resource Management in Java 7

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

Title: Complete the code about the exception handling

Difficulty: Medium

Section Reference 1: 11.5 Application: Handling Input Errors

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

Title: Complete the code about the exception handling

Difficulty: Medium

Section Reference 1: 11.5 Application: Handling Input Errors

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

Title: Which statement about this code is correct?

Difficulty: Hard

Section Reference 1: 11.5 Application: Handling Input Errors

111. Select the missing expression in the code fragment below. The method should continue to prompt the user for a valid integer value until one is entered. The method returns the final value entered.

public static int getAge()

{

boolean done = false;

Scanner console = new Scanner(System.in);

int value = 0;

while (!done)

{

try

{

System.out.print("Please enter your age in years: ");

value = console.nextInt();

done = true;

}

______________________

{

System.out.println("Invalid value. Try again.");

console.nextLine();

}

}

return value;

}

a) finally

b) catch (IllegalArgumentException exception)

c) catch (NoSuchElementException exception)

d) catch (NumberFormatException exception)

Title: Complete method to prompt user for valid integer value

Difficulty: Medium

Section Reference 1: 11.5 Application: Handling Input Errors

Document Information

Document Type:
DOCX
Chapter Number:
11
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 11 Input/Output and Exception Handling
Author:
Cay S. Horstmann

Connected Book

Big Java Early Objects 5e Complete Test Bank

By Cay S. Horstmann

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party