Chapter 7 part 1

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

25) Which of the following statements about using a PrintWriter object is NOT true? s.a) A PrintWriter will be automatically closed when the program exit b) Data loss may occur if a program fails to close a PrintWriter object before exiting. c) PrintWriter is an enhancement of the PrintStream class. d) A program can write to a PrintWriter using println

A PrintWriter will be automatically closed when the program exit

36) Which return value of the JFileChooser object's showOpenDialog method indicates that a file was chosen by the user at run time? a) APPROVE_OPTION b) CANCEL_OPTION c) OK_OPTION d) SELECTED_OPTION

a) APPROVE_OPTION

5) Under which condition will the PrintWriter constructor generate a FileNotFoundException? a) If the output file cannot be opened or created due to a security error. b) If the output file does not exist. c) If the output file already exists, but has data in it. d) If the output file already exists, but is empty.

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

9) Which of the following objects should be used for reading from a text file? a) Scanner b) ReadStream c) PrintStream d) ReadFile

a) Scanner

21) Consider the following code snippet. Scanner inputFile = new Scanner("dataIn.txt"); Which of the following statements is correct? a) This code will not open a file named "dataIn.txt", but will treat the string "dataIn.txt" as an input value. b) This code will create a new file named "dataIn.txt". c) This code will open an existing file named "dataIn.txt" for reading. d) This code will open a file named "dataIn.txt" for writing.

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

42) 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? a) This code will read in a word at a time from the input file. b) This code will read in the entire input file in one operation. c) This code will read in a line at a time from the input file. d) This code will read in a character at a time from the input file.

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

22) Consider the following code snippet. Scanner inputFile = new Scanner("hoursWorked.txt"); Which of the following statements is correct? a) This code will treat the string "hoursWorked.txt" as an input value. b) This code will create a new file named "hoursWorked.txt". c) This code will open an existing file named "hoursWorked.txt" for reading. d) This code will open a file named "hoursWorked.txt" for writing

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

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) . . . } a) new File(inputFileName) b) new File(outputFileName) c) new File(inputFile) d) new File(System.in)

a) new File(inputFileName)

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 = _________; . . . } a) new File(inputFileName) b) new File("dataIn.txt") c) new Scanner(inputFile) d) new Scanner("dataIn.txt")

a) new File(inputFileName)

3) Insert the missing code in the following code fragment. This fragment is intended to read a file and write to a file. 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 = _____________; . . . } a) new PrintWriter(outputFileName) b) new Scanner(outputFileName) c) new PrintWriter(outputFile) d) new Scanner(outputFile

a) new PrintWriter(outputFileName)

27) 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 main method? a) public static void main(String[] args) throws IOException b) public static void main(String[] args) throws FileNotFoundException c) public static void main(String[] args) d) public static void main(String[] args) throws UnknownFileException

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

12) Which of the following statements about a PrintWriter object is true? a) A PrintWriter will be automatically closed when the program exits. b) Data loss may occur if a program fails to close a PrintWriter object before exiting. c) No data loss will occur if the program fails to close a PrintWriter before exiting. d) An exception will occur if the program fails to close a PrintWriter before exiting.

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

8) Which of the following is the correct syntax for creating a File object? a) File inFile = File("input.txt") b) File inFile = new File("input.txt") c) File inFile = File.open("input.txt") d) File inFile = new File.open("input.txt")

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

23) Consider the following code snippet. PrintWriter outFile = new PrintWriter("dataOut.txt"); Which of the following statements about the PrintWriter object is correct? a) If a file named "dataOut.txt" already exists, an exception will occur. b) If a file named "dataOut.txt" already exists, existing data will be deleted before new data is added to the file. c) If a file named "dataOut.txt" already exists, new data will be added to the end of the file. d) If a file named "dataOut.txt" already exists, a new file named "dataOut_1.txt" will be created and used

b) If a file named "dataOut.txt" already exists, existing data will be deleted before new data is added to the file.

24) Consider the following code snippet. PrintWriter outputFile = new PrintWriter("payrollReport.txt"); Which of the following statements about the PrintWriter object is correct? a) If a file named "payrollReport.txt" already exists, an exception will occur. b) If a file named "payrollReport.txt" already exists, existing data will be deleted before new data is added to the file. c) If a file named "payrollReport.txt" already exists, new data will be added to the end of the file. d) If a file named "payrollReport.txt" already exists, a new file named "payrollReport_1.txt" will be created and used.

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

6) Under which condition will the Scanner constructor generate a FileNotFoundException? a) If the input file cannot be opened due to a security error. b) If the input file does not exist. c) If the input file already exists, but has data in it. d) If the input file already exists, but is empty

b) If the input file does not exist

43) When reading words using a Scanner object's next method, ____. a) any characters at the beginning of the input that are considered to be white space are consumed and become part of the word being read. b) 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. c) the program must discard white space characters at the beginning of the input before calling the next method. d) any characters that are considered to be white space within the word become part of the word.

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

13) 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? a) public static void main(String[] args) throws FileMissingException b) public static void main(String[] args) throws FileNotFoundException c) public static void main(String[] args) d) public static void main(String[] args) throws UnknownFileException

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

26) Your program must 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? a) public static void main(String[] args) throws FileMissingException b) public static void main(String[] args) throws FileNotFoundException c) public static void main(String[] args) d) public static void main(String[] args) throws UnknownFileException

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

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

b) throws FileNotFoundException

48) 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? a) Only alphabetic characters will be ignored. b) Only numeric characters will be ignored. c) Characters that are neither alphabetic nor numeric will be ignored. d) Both alphabetic and numeric characters will be ignored

c) Characters that are neither alphabetic nor numeric will be ignored.

15) Consider the following code snippet: PrintWriter out = new PrintWriter("output.txt"); Which of the following statements about the PrintWriter object is correct? a) If a file named "output.txt" already exists, an exception will occur. b) If a file named "output.txt" already exists, data will be added at the end of the file. c) If a file named "output.txt" already exists, existing data will be deleted before data are added to the file. d) If a file named "output.txt" already exists, a new file named "output_1.txt" will be created and used.

c) If a file named "output.txt" already exists, existing data will be deleted before data are added to the file.au

4) Which of the following statements about using the PrintWriter object is correct? a) If the output file already exists, the new data will be appended to the end of the file. b) If the output file does not exist, a FileNotFoundException will occur. c) If the output file already exists, the existing data will be discarded before new data are written into the file. d) If the output file does not exist, an IllegalArgumentException will occur.

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

45) Which of the following statements about white space in Java is correct? a) In Java, white space includes spaces only. b) In Java, white space includes spaces and tab characters only. c) In Java, white space includes spaces, tab characters, and newline characters. d) In Java, white space includes spaces, tab characters, newline characters, and punctuation

c) In Java, white space includes spaces, tab characters, and newline characters

Scanner in = new Scanner(. . .); in.useDelimiter("[^A-Za-z]+"); What characters will be ignored and not read in when using this code? a) Only alphabetic characters will be ignored. b) Only numeric characters will be ignored. c) Only non-alphabetic characters will be ignored. d) Only non-numeric characters will be ignored

c) Only non-alphabetic characters will be ignored.

16) The PrintWriter class is an enhancement of the ____ class. a) Scanner b) ReadStream c) PrintStream d) File

c) PrintStream

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. Which of the following is the correct syntax for doing this? a) Scanner in = new Scanner("hoursWorked.txt"); b) Scanner in = Scanner("hoursWorked.txt"); c) Scanner in = new Scanner(hoursFile); d) Scanner in = Scanner.open(hoursFile);

c) Scanner in = new Scanner(hoursFile);

10) Consider the following code snippet: public static void main(String[] args) throws FileNotFoundException Which of the following statements about this code is correct? a) The main method is designed to catch and handle all types of exceptions. b) The main method is designed to catch and handle the FileNotFoundException. c) The main method should simply terminate if the FileNotFoundException occurs. d) The main method will not terminate if any exception occurs.

c) The main method should simply terminate if the FileNotFoundException occurs.

11) Consider the following code snippet: public static void main(String[] args) throws IOException Which of the following statements about this code is correct? a) The main method is designed to catch and handle all types of exceptions. b) The main method is designed to catch and handle the IOException. c) The main method should simply terminate if the IOException occurs. d) The main method will not terminate if any exception occurs.

c) The main method should simply terminate if the IOException occurs.

33) Which of the following statements about reading web pages is true? a) A Scanner object cannot be used to read a web page. b) You must create a File object to use with a Scanner object to read a web page. c) You must create a URL object to use with a Scanner object to read a web page. d) You must use the openStream() method of a Scanner object to read a web page.

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

38) Which of the following statements about reading and writing binary data is correct? a) You use the Scanner class to read and write binary data. b) You use the PrintWriter class to read and write binary data. c) You use the InputStream class to read binary data and the FileOutputStream class to write binary data. d) You use the InputStream class to write binary data and the FileOutputStream class to read binary data

c) You use the InputStream class to read binary data and the FileOutputStream class to write binary data.

17) Which of the following statements about reading and writing text files is correct? a) You use the Scanner class to read and write text files. b) You use the PrintWriter class to read and write text files. c) You use the Scanner class to read text files and the PrintWriter class to write text files. d) You use the Scanner class to write text files and the PrintWriter class to read text files.

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

35) Which method of the JFileChooser object will return the file object that describes the file chosen by the user at runtime? a) getFile() b) getSelectedFilePath() c) getSelectedFile() d) getFilePath()

c) getSelectedFile()

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

c) in.hasNext()

49) 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); } a) in.getNext() b) in.next() c) in.next.charAt(0) d) in.nextChar()

c) in.next.charAt(0)

31) 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? a) inputFile = new File("c:\java\myProg\input.txt"); b) inputFile = new File.open("c:\java\myProg\input.txt"); c) inputFile = new File("c:\\java\\myProg\\input.txt"); d) inputFile = new File.open("c:\\java\\myProg\\input.txt");

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

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

c) new Scanner(inputFile)

50) 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? a) Scanner in = new Scanner(. . .); in.useDelimiter("[^A-Za-z]+"); b) Scanner in = new Scanner(. . .); in.useDelimiter("[A-Za-z]+"); c) Scanner in = new Scanner(. . .); in.useDelimiter("[^0-9]+"); d) Scanner in = new Scanner(. . .); in.useDelimiter("");

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

41) When reading words with a Scanner object, a word is defined as ____. a) Any sequence of characters consisting of letters only. b) Any sequence of characters consisting of letters, numbers, and punctuation symbols only. c) Any sequence of characters consisting of letters and numbers only. d) Any sequence of characters that is not white space.

d) Any sequence of characters that is not white space.

7) Which of the following statements reflects the textbook's recommendations about closing files? a) Both the input and the output file do not need to be explicitly closed in the program. b) Only the input file must be explicitly closed in the program. c) Only the output file must be explicitly closed in the program. d) Both the input and the output file should be explicitly closed in the program

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

34) Which Java class implements a file dialog box for selecting a file by a program with a graphical user interface? a) JOptionPane b) JFileOption c) JFilePane d) JFileChooser

d) JFileChooser

47) 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? a) Only alphabetic characters will be ignored. b) Only numeric characters will be ignored. c) Only non-alphabetic characters will be ignored. d) Only non-numeric characters will be ignored.

d) Only non-numeric characters will be ignored.

14) Consider the following code snippet: File inputFile = new File("input.txt"); You wish to read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this? a) Scanner in = Scanner ("input.txt") b) Scanner in = new Scanner ("input.txt") c) Scanner in = Scanner.open(inputFile) d) Scanner in = new Scanner(inputFile)

d) Scanner in = new Scanner(inputFile)

37) 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); . . . } a) chooser.getFileName() b) chooser.getSelectedFileName() c) chooser.getFilePath() d) chooser.getSelectedFile()

d) chooser.getSelectedFile()

39) Insert the missing code in the following code fragment. This fragment is intended to read binary data. public static void main(String[] args) throws IOException { String address = "http://horstmann.com/bigjava.gif"; URL imageLocation = new URL(address); InputStream in = _________; . . . } a) new Scanner("http://horstmann.com/bigjava.gif") b) new Scanner(imageLocation) c) new imageLocation.openStream d) imageLocation.openStream()

d) imageLocation.openStream()

30) 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); . . . } a) new File(c:/payroll/"dataIn.txt") b) new File(c://payroll//"dataIn.txt") c) new File("c:\payroll\dataIn.txt") d) new File("c:\\payroll\\dataIn.txt")

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

29) 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 = _______; . . . } a) new PrintWriter("c:/reports/dataOut.txt") b) new PrintWriter("c://reports//dataOut.txt") c) new PrintWriter("c:\reports\dataOut.txt") d) new PrintWriter("c:\\reports\\dataOut.txt")

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

32) 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 = _________; . . . } a) new Scanner("http://horstmann.com/index.html") b) new Scanner(pageLocation) c) new Scanner(new File(pageLocation)) d) new Scanner(pageLocation.openStream())

d) new Scanner(pageLocation.openStream())

40) Insert the missing code in the following code fragment. This fragment is intended to read binary data. public static void main(String[] args) throws IOException { String address = "http://horstmann.com/bigjava.gif"; URL imageLocation = _________; InputStream in = imageLocation.openStream()); . . . } a) new Scanner("http://horstmann.com/bigjava.gif") b) new Scanner(address) c) new imageLocation.openStream d) new URL(address)

d) new URL(address)

28) 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); . . . } a) catch FileMissingException b) catch FileNotFoundException c) throws FileMissingException d) throws FileNotFoundException

d) throws FileNotFoundException


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

Cardiovascular system Circulation

View Set

Chapter 1: The Scientific Stiudy of LIfe

View Set

The Solar System: Terrestrial Planets

View Set