13
AP Computer Science A Chapter 11 Test Bank 1. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt. public static void main(String[] args) throws FileNotFoundException { String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = _______________; . . . } 2. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt. public static void main(String[] args) __________________ { String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); . . . } 3. 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 = _____________; . . . } 4. Which of the following statements about using the PrintWriter object is correct? A. If the output file does not exist, a FileNotFoundException will occur. B. If the output file already exists, the new data will be appended to the end of the file. C. If the output file does not exist, an IllegalArgumentException will occur. D. If the output file already exists, the existing data will be discarded before new data are written into the file.

Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

Embed Size (px)

Citation preview

Page 1: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

AP Computer Science AChapter 11 Test Bank

1. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt.public static void main(String[] args) throws FileNotFoundException{ String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = _______________; . . .}

2. Insert the missing code in the following code fragment. This fragment is intended to read an input file named dataIn.txt.public static void main(String[] args) __________________{ String inputFileName = "dataIn.txt"; File inputFile = new File(inputFileName); Scanner in = new Scanner(inputFile); . . .}

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

4. Which of the following statements about using the PrintWriter object is correct?A. If the output file does not exist, a FileNotFoundException will occur.B. If the output file already exists, the new data will be appended to the end of the file.C. If the output file does not exist, an IllegalArgumentException will occur.D. If the output file already exists, the existing data will be discarded before new data are

written into the file.5. Under which condition will the PrintWriter constructor generate

a FileNotFoundException?6. Under which condition will the Scanner constructor generate

a FileNotFoundException?7. What reflects the recommendations about closing files?8. What is the correct syntax for creating a File object?9. Which objects should be used for reading from a text file?10. Consider the following code snippet:

public static void main(String[] args) throws FileNotFoundExceptionWhat is the purpose of the throw exception above?

11. Which of the following statements about a PrintWriter object is true?A. A PrintWriter will be automatically closed when the program exits.

Page 2: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

B. No data loss will occur if the program fails to close a PrintWriter before exiting.C. An exception will occur if the program fails to close a PrintWriter before exiting.D. Data loss may occur if a program fails to close a PrintWriter object before exiting.

12. Your program will read in an existing text file. You want the program to terminate if the file does not exist. Write a program that indicates the correct code for the main method header.

13. Consider the following code snippet:File inputFile = new File("input.txt");You wish to read the contents of this file using a Scanner object. What is the correct syntax for doing this?

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

15. Consider the following code snippet:PrintWriter out = new PrintWriter("output.txt");If a file named "output.txt" already exists, what will the program do with the file?

16. The PrintWriter class is an enhancement of the ____ class.17. What is the purpose of the Scanner and PrintWriter classes?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); . . .}

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

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. What is the correct syntax for doing this?

21. Which of the following statements about using a PrintWriter object is NOT true?A. PrintWriter is an enhancement of the PrintStream class.B. A program can write to a PrintWriter using println.C. A PrintWriter will be automatically closed when the program exits.D. Data loss may occur if a program fails to close a PrintWriter object before exiting.

Page 3: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

22. Your program must read in an existing text file. You want the program to terminate if any exception related to the file occurs. What is the correct code for the header of the main method?

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

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

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

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

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

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

29. When reading words with a Scanner object, a word is defined as ____.30. Consider the following code snippet.

Page 4: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

File inputFile = new File("dataIn.txt"); Scanner in = new Scanner(inputFile);while (in.hasNext()){ String input = in.next();}Describe how the code will read the file.

31. When reading words using a Scanner object's next method, ____.32. 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);}

33. In Java, white space is defined as _______________.34. 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);}

35. What pattern should be used for the delimiter to read one character at a time using a Scanner object's next method?

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

37. The ____ method of the Character class will indicate if a character contains white space.38. Consider the following code snippet:

Scanner in = new Scanner(. . .);while (in.hasNextLine()){ String input = in.nextLine(); System.out.println(input);}Describe how the code will read the file.

39. Which String class method will remove spaces from the beginning and the end of a string?40. 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);

Page 5: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

while (_________________) { word = words.next(); } System.out.println(word);}

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

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

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

44. Consider the following code snippet.Scanner in = new Scanner(. . .);while (in.hasNextDouble()){ double input = in.nextDouble();}Describe how the code will read the file.

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

46. 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?

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

48. Consider the following code snippet:Scanner in = new Scanner(. . .);String result = "";int number = 0;

Page 6: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

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?

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

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

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

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

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

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

55. 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?

56. What is the purpose of the throw statement?

Page 7: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

57. 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){ ___________________________________________}

58. Consider the following code snippet:throw IllegalArgumentException("This operation is not allowed!");What is the result of the statement above?

59. Consider the following code snippet:throw new IllegalArgumentException("This operation is not allowed!");

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

61. Which of the following statements about exception reporting is true?A. Use the throw statement to report that an exception has occurred.B. Use the reportError statement to report that an exception has occurred.C. Use the reportException statement to report that an exception has occurred.D. Use the throwException statement to report that an exception has occurred.

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

63.Where should statements that may cause an exception be placed? 64. What happens if an exception has no handler?65. Which method of an exception object will provide information about the chain of method calls

that led to the exception?66. 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();}

67. Which method of an exception object will retrieve a description of the exception that occurred?68. Select an appropriate expression to complete the header for the method below.

public void openFile(String inputFile) ______________________________{ File theFile = new File(inputFile);

Page 8: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

Scanner data = new Scanner(theFile); // additional statements to input data from file}

69. What is the main difference between checked and unchecked exceptions?70. The Java compiler requires that your program handle which type of exceptions?71. If the current method in a program will not be able to handle an exception, what should be coded

into the method?72. 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, what will occur?

73. An example of a fatal error that rarely occurs and is beyond your control is the ____.74. 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. The user must decide whether to continue or terminate the program.B. The current method must decide whether to continue or terminate.C. The current method terminates immediately.D. Execution will continue with the next statement in the method.

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

76. 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?

A. Any exceptions that occur in this method will be suppressed.B. This method will pass any IOException-type exception back to the caller.C. The method will never close the file if an IOException exception occurs in this

method.D. This method will pass any type of exception back to the caller.

77. Which of the following statements about exception handling is recommended?A. Throw an exception as soon as a problem is detected, but only catch exceptions when the

problem can be handled. B. All exceptions should be handled at the top of the chain of methods.C. All exceptions should be handled where they are first detected.

Page 9: Web viewThe _____ method of the Scanner class specifies a pattern for word boundaries when ... [A-Za-z ]+"); What characters ... The Java compiler requires that

D. Throw an exception only when the problem can be handled.78. 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?

A. This code will not catch a FileNotFoundException that occurs in the try block.B. This code will not catch any exceptions that occur in the try block.C. This code will pass any exceptions back to its caller.D. This code will catch exceptions that occur in the try block but will do nothing about the

exceptions.79. 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);}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 writeData() statement throws an exception.C. The file will be closed only if the PrintWriter constructor throws an exception.D. It is not possible to determine whether the file will be closed if an exception occurs.

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

A. The close method of the outputFile object will be automatically invoked when the try block ends, but only if no exception occurred.

B. The program will terminate with an unhandled exception if the PrintWriter constructor fails.

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

D. The close method of the outputFile object will be automatically invoked when the try block ends, but only if an exception occurs.

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

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