Exam 11: Inputoutput and Exception Handling

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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

Free
(Multiple Choice)
4.9/5
(40)
Correct Answer:
Verified

B

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

Free
(Multiple Choice)
4.9/5
(32)
Correct Answer:
Verified

C

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

Free
(Multiple Choice)
4.7/5
(36)
Correct Answer:
Verified

D

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?

(Multiple Choice)
4.9/5
(33)

When writing a method, which of the following statements about exception handling is true?

(Multiple Choice)
4.7/5
(39)

Which of the following statements about exception handling is recommended by the textbook?

(Multiple Choice)
4.7/5
(37)

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); }

(Multiple Choice)
4.7/5
(31)

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?

(Multiple Choice)
4.8/5
(35)

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

(Multiple Choice)
4.8/5
(43)

What is the purpose of the throw statement?

(Multiple Choice)
4.8/5
(28)

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?

(Multiple Choice)
5.0/5
(34)

Consider the following code snippet: public static void main(String[] args) throws IOException Which of the following statements about this code is correct?

(Multiple Choice)
4.8/5
(33)

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?

(Multiple Choice)
4.9/5
(47)

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?

(Multiple Choice)
4.8/5
(31)

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?

(Multiple Choice)
4.7/5
(43)

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); }

(Multiple Choice)
4.9/5
(46)

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.

(Multiple Choice)
4.7/5
(37)

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 { ___________________ } }

(Multiple Choice)
4.8/5
(34)

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?

(Multiple Choice)
4.8/5
(40)

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

(Multiple Choice)
4.7/5
(30)
Showing 1 - 20 of 109
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)