Chapter 11

¡Supera tus tareas y exámenes ahora con Quizwiz!

public void String readFile() throws IOException { File inputFile = new File(. . .); try __________________________________ { while (in.hasNext()) { . . . } } }

(Scanner in = new Scanner(inputFile))

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

APPROVE_OPTION

Which of the following statements about assertions is NOT true?

An assertion is always checked during program execution.

If there is no integer number appearing next in the input, what will occur?

An exception will occur.

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.

Which of the following statements about command line arguments is correct?

Command line arguments can be read using the main method's args parameter.

Which of the following statements about a PrintWriter object is true?

Data loss may occur if a program fails to close a PrintWriter object before exiting.

Which expression converts the string input containing floating-point digits to its floating-point value?

Double.parseDouble(input)

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 about the PrintWriter object is correct? PrintWriter outputFile = new PrintWriter("payrollReport.txt");

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

Under which condition will the Scanner constructor generate a FileNotFoundException?

If the input file does not exist.

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

If the output file already exists, the existing data will be discarded before new data are written into the file.

Under which condition will the PrintWriter constructor generate a FileNotFoundException?

If the output file cannot be opened or created due to a security error.

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

IllegalArgumentException

Which exception class should you use from the standard library if you want to thrown an exception when there are insufficient funds in a bank account to cover a withdrawal?

IllegalArgumentException

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)

What is the purpose of the throw statement?

It is used to pass control to an error handler when an error situation is detected.

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

JFileChooser

Which of the following statements about character encodings is NOT true?

Java assumes the UTF-8 encoding.

Select an expression to complete the program segment below, which displays an error message and terminates normally if the 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"); }

NumberFormatException exception

What characters will be ignored and not read in when using this code?

Only non-alphabetic characters will be ignored.

Scanner in = new Scanner(. . .); in.useDelimiter("[^0-9]+"); What characters will be ignored and not read in using this code?

Only non-numeric characters will be ignored.

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

OutOfMemoryError

The PrintWriter class is an enhancement of the ____ class.

PrintStream

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

Scanner

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?

Scanner in = new Scanner(. . .); in.useDelimiter("");

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

You wish to 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(inputFile);

Which of the following statements about exception handling is correct?

Statements that may cause an exception should be placed within a try block.

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.

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

Which of the following statements about checked and unchecked exceptions is true?

The compiler ensures that the program is handling checked exceptions.

Consider the following code snippet, assuming that filename represents the name of the output file and writeData outputs the data to that file:

The file will be closed regardless of when an exception occurs.

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

The main method terminates if the FileNotFoundException occurs.

String line = ...; Scanner lineScanner = new Scanner(line);

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

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.

Consider the following code snippet, assuming in is an instantiated Scanner:

The program will throw an exception if there is data left in the input when the if statement is executed.

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

The throws clause must list all checked exceptions that this method may throw, and may also list unchecked exceptions.

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.

throw new IllegalArgumentException("This operation is not allowed!");

This code constructs an object of type IllegalArgumentException and throws the object.

File inputFile = new File(filename); try (Scanner in = new Scanner(inputFile)) { . . . } catch (Exception e) { }

This code will catch exceptions that occur in the try block but will do nothing about the exceptions.

throw IllegalArgumentException("This operation is not allowed!");

This code will not compile.

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

This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value.

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

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

File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile); while (in.hasNext()) { String input = in.next(); }

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

Scanner in = new Scanner(. . .); while (in.hasNextLine()) { String input = in.nextLine(); System.out.println(input); }

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

Scanner in = new Scanner(. . .); while (in.hasNextDouble()) { double input = in.nextDouble(); }

This code will read in one floating point value at a time from the input file.

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

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

If the file cannot be located, which of the following statements about this code is correct? public void readFile(String filename) throws FileNotFoundException { File inFile = new File(filename); Scanner in = new Scanner(inFile); . . . }

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

Consider the following code snippet, where data is a variable defined as a double array that is populated by the readData method for valid data:

This method will pass any IOException-type exception back to the caller.

Which of the following statements about exception handling is recommended?

Throw an exception as soon as a problem is detected, but only catch exceptions when the problem can be handled.

Which of the following statements about checked and unchecked exceptions is NOT true?

Unchecked exceptions belonging to subclasses of the RunTimeException class are beyond the control of the programmer.

You have opened a command prompt window and you have entered the following: java myProg Bob Smith

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

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

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

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

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

When reading words using a Scanner object's next method, ____.

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

When reading words with a Scanner object, a word is defined as ____.

any sequence of characters that is not white space.

When you start a Java program from a command line and supply argument values, the values ____.

are stored as String values

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

args.length > 0

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.

catch (IOException exception)

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.

catch (NoSuchElementException exception)

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

checked

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

chooser.getSelectedFile()

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

getMessage()

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

Insert the missing code in the following code fragment. This fragment is intended to read all words from a text file named dataIn.txt.

in.hasNext()

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

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.

inputFile.next()

Which command enables assertion checking during program execution?

java -enableassertions MainClass

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?

java myProg arg1 arg2

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.

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

File inputFile = _________________;

new File(inputFileName)

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.

new PrintWriter("dataout.txt")

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

new Scanner(inputFile)

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.

new Scanner(inputFile)

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

new Scanner(pageLocation.openStream())

If the input file begins with the characters 626.14 average, what values will number and result have after this code is executed?

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

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 method of an exception object will provide information about the chain of method calls that led to the exception?

printStackTrace()

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?

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

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 header of the main method?

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

Which of the following code snippets about exceptions is correct?

public void read(String filename) throws IOException, ClassNotFoundException

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

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

Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt.

throws FileNotFoundException

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

throws FileNotFoundException

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 }

throws FileNotFoundException

public void String readFile() _________________ { File inputFile = new File(. . .); try (Scanner in = new Scanner(inputFile)) { while (in.hasNext()) { . . . } } }

throws IOException

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

trim()

The ______ method of the Scanner class specifies a pattern for word boundaries when reading text.

useDelimiter()

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


Conjuntos de estudio relacionados

Practial 2 Muscles of the Head and Anterior Neck

View Set

SOC 321: Sociological Theory Final

View Set

Chapter 15: Prenatal Diagnostic Tests

View Set

Listening InQuizitive 23: Symphony No. 5 in C Minor, Op. 67, I

View Set

Bar: MBE questions FOR REAL PROPERTY

View Set

pre-ch 11 Differential Analysis: The Key to Decision Making

View Set

Quiz 35-Linear Regression and r-squared

View Set

ATI Fundamentals Fundamentals Review 2019 *

View Set