Object-Oriented Computing - Ch 7

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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?

An exception will occur.

Consider the following code snippet. PrintWriter outputFile = new PrintWriter("payrollReport.txt"); Which of the following statements about the PrintWriter object is correct?

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

Which statement about handling exceptions is true?

If an exception has no handler, the program will be terminated.

Assuming that the string input contains the digits of an integer, without any additional characters, which expression obtains the corresponding numeric value?

Integer.parseInt(input)

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

OutOfMemoryError

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?

Scanner in = new Scanner(hoursFile);

Assume inputFile is a Scanner object used to read data from a text file that 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 to display double value }

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

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

The main method terminates if the FileNotFoundException occurs.

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

The main method terminates if the IOException occurs.

Consider the following code snippet. String line = ...; Scanner lineScanner = new Scanner(line); Which of the following statements about this code is correct?

The methods of the Scanner class can be used on lineScanner to scan the string.

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?

The program should declare the PrintWriter variable in a try-with-resources statement to ensure that the file is closed.

If the current method in a program will not be able to handle an exception, what should be coded into the method?

The throws clause should list the names of all exceptions that the method will not handle.

Consider the following code snippet, assuming that description is a String and totalPrice is a double: System.out.printf("%-12s%8.2f",description,totalPrice); Which of the following statements is correct?

This code will produce 2 columns, with the description field left justified and the totalPrice field right justified.

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?

This code will read in a word at a time from the input file.

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?

This code will read in an entire line from the file in each iteration of the loop.

Consider the following code snippet. Scanner inputFile = new Scanner("hoursWorked.txt"); Which of the following statements is correct?

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

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?

This method will be terminated if the file cannot be located.

You have opened a command prompt window and you have entered the following: java myProg Bob Smith Which of the following statements is correct?

You have supplied two argument values, and these values can be accessed in the main method using the args parameter.

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

hasNextInt()

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?

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

Assume inputFile is a Scanner object used to read data from a text file that 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);

inputFile.hasNextInt()

Consider the following code snippet: Scanner in = new Scanner(. . .); String result = ""; double number = 0; if (in.hasNextDouble()) { number = in.nextDouble(); } result = in.next(); If the input file contains the characters 626.14 average, what values will number and result have after this code is executed?

number will contain the value 626.14 and result will contain the value average.

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

trim()

Assume inputFile is a Scanner object used to read data from a text file that 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); }

words.hasNext()

Consider the following code snippet: PrintWriter out = new PrintWriter("output.txt"); If a file named "output.txt" already exists, which of the following statements about the PrintWriter object is correct?

Existing data will be deleted before data are added to the file.

Which of the following statements reflects the recommendations about closing files?

Both the input and the output file should be explicitly closed in the program.


संबंधित स्टडी सेट्स

American Public University Finc 400 Midterm

View Set

Lesson 3: Review of Solving Equations

View Set

ATI MENTAL HEALTH STUDY QUESTIONS

View Set

Gruber Chapter 9/Stiglitz Chapter 6

View Set

Ricci, Kyle & Carman: Maternity and Pediatric Nursing, Second Edition Chapter 20: Nursing Management of the Pregnancy at Risk: Selected Health Conditions and Vulnerable Populations; prepU

View Set