Java final (ch. 11)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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(. . .); try __________________________________ { while (in.hasNext()) { . . . } } }

(Scanner in = new Scanner(inputFile))

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

OutOfMemoryError

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: throw IllegalArgumentException("This operation is not allowed!"); Which of the following statements about this code is correct?

This code will not compile.

Which of the following statements about exception reporting is true?

Use the throw statement to report that an exception has occurred.

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.

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

checked

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

useDelimiter()

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 assertion is NOT true?

An assertion is always checked during program execution

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

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

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

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. while (inputFile.hasNextDouble()) { double value = inputFile.nextDouble(); ___________________________________ // statement to display double value }

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

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

The "close" method of the "outputFile" object will be automatically invoked when the "try" block ends, whether or not an exception has occurred.

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

The compiler ensures that the program is handling checked exceptions.

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?

The current method terminates immediately.

Which of the following statements about the try/finally statement is NOT true?

The finally clause is executed after the exception is propagated to its handler.

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.

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.

Consider the following code snippet, assuming in is an instantiated Scanner: if (in.hasNext()) { throw new IOException("End of file expected"); } Which of the following statements about this code is correct?

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.

Consider the following code snippet: throw new IllegalArgumentException("This operation is not allowed!"); Which of the following statements about this code is correct?

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

Consider the following code snippet: throw new IllegalArgumentException("This operation is not allowed!"); Which of the following statements about this code is correct?

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

Consider the following code snippet: File inputFile = new File(filename); try (Scanner in = new Scanner(inputFile)) { . . . } catch (Exception e) { } Which of the following statements about this code is correct?

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

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

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); 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 in = new Scanner(. . .); while (in.hasNextDouble()) { double input = in.nextDouble(); } Which of the following statements about this code is correct?

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

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: public double[] readInputFile(String filename) throws IOException { try (Scanner in = new Scanner(new File(filename))) { readData(in); return data; } } Which of the following statements about this method's code is correct?

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.

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.

What is recommended if the standard library does not have an exception class that describes your particular error situation?

Design your own exception class as a subclass of an existing exception class.

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

Double.parseDouble(input)

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 is the correct syntax for creating a File object?

File inFile = new File("input.txt");

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.

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.

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)

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

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

NumberFormatException exception

Consider the following code snippet: Scanner in = new Scanner(. . .); in.useDelimiter("[A-Za-z]+"); What characters will be read in using this code?

Only non-alphabetic characters will be read in.

The PrintWriter class is an enhancement of the ____ class.

PrintStream

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.

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

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. String filename = . . .; try (Scanner in = new Scanner(new File(filename))) { . . . } ___________________ { exception.printStackTrace(); }

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

catch (NoSuchElementException exception)

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

chooser.getSelectedFile()

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

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

getSelectedFile()

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

which statement about handling exceptions is true?

if an exception has no handler, the program will be terminated

Insert the missing code in the following code fragment. This fragment is intended to read all words from a text file named dataIn.txt. File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile); while (____________) { String input = in.next(); System.out.println(input); }

in.hasNext()

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

in.hasNextDouble()

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

in.next().charAt(0)

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

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

isWhiteSpace()

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 command enables assertion checking during program execution?

java -enableassertions MainClass

which of the following is 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. public static void main(String[] args) throws FileNotFoundException { File inputFile = ________; Scanner in = new Scanner(inputFile); . . . }

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

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 = _______; . . . }

new PrintWriter("c:\\reports\\dataOut.txt")

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 = _______________________;

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. 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 = _____________; . . . }

new PrintWriter(outputFileName)

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 = _________; PrintWriter outFile = new PrintWriter("dataOut.txt"); . . . }

new Scanner(inputFile)

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

new Scanner(pageLocation.openStream())

Scanner in = new Scanner(. . .); String result = ""; int number = 0; if (in.hasNextInt()) { number = in.nextInt(); } result = in.next(); 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.

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.

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?

only non-numeric characters will be ignored.

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

Consider the following code snippet, assuming that filename represents the name of the output file and writeData outputs the data to that file: try (PrintWriter outputFile = new PrintWriter(filename)) { writeData(outputFile); } Which of the following statements about this code is correct? You Answered

the close method of the outputFile object will be automatically invoked when the try block ends, whether or not an exception has occurred.

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.

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 I0Exception occurs

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.

consider the following code snippet: "throw new IllegalArgumentException("This operation is not allowed!"); which of the following statements about this code is correct?

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

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

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

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(. . .); try (Scanner in = new Scanner(inputFile)) { while (in.hasNext()) { . . . } } }

throws I0Exception

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

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

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.


Ensembles d'études connexes

Unit 3: IV Therapy: Complex Therapies and Medications

View Set