Java File I/O Multiple Choice Questions and Answers - Prof. Susanna Wei, Exams of Computer Science

Multiple choice questions and answers related to java file i/o. It covers topics such as input and output streams, file paths, exceptions, and methods for reading and writing files. Students and developers can use this document to test their knowledge or as a study aid for learning java file i/o.

Typology: Exams

Pre 2010

Uploaded on 08/16/2009

koofers-user-vq6
koofers-user-vq6 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 10: File I/O
Multiple Choice
1) An ___________ allows data to flow into your program.
(a) input stream
(b) output stream
(c) file name
(d) all of the above
2) An ____________ allows data to flow from your program.
(a) input stream
(b) output stream
(c) file name
(d) all of the above
3) The output stream connected to the computer screen is:
(a) System.exit
(b) System.quit
(c) System.in
(d) System.out
4) In Java, when you open a text file you should account for a possible:
(a) FileNotFoundException
(b) FileFullException
pf3
pf4
pf5

Partial preview of the text

Download Java File I/O Multiple Choice Questions and Answers - Prof. Susanna Wei and more Exams Computer Science in PDF only on Docsity!

Chapter 10: File I/O

Multiple Choice

  1. An ___________ allows data to flow into your program. (a) input stream (b) output stream (c) file name (d) all of the above
  2. An ____________ allows data to flow from your program. (a) input stream (b) output stream (c) file name (d) all of the above
  3. The output stream connected to the computer screen is: (a) System.exit (b) System.quit (c) System.in (d) System.out
  4. In Java, when you open a text file you should account for a possible: (a) FileNotFoundException (b) FileFullException

(c) FileNotReadyException (d) all of the above

  1. There are two common classes used for reading from a text file. They are: (a) PrintWriter and BufferedReader (b) FileInputStream and Scanner (c) BufferedReader and Scanner (d) None of the above
  2. The scanner class has a series of methods that checks to see if there is any more well- formed input of the appropriate type. These methods are called __________ methods: (a) nextToken (b) hasNext (c) getNext (d) testNext
  3. All of the following are methods of the Scanner class except: (a) nextFloat() (b) next() (c) useDelimiter() (d) readLine()
  4. The method _________ reads a single character from an input stream. (a) readLine() (b) skip() (c) read()

(d) getName()

  1. The class ObjectOutputStream contains all of the following methods except: (a) writeInt() (b) writeChar() (c) writeDouble() (d) println()
  2. The method __________ from the File class forces a physical write to the file of any data that is buffered. (a) close() (b) flush() (c) writeUTF() (d) writeObject()
  3. The class ObjectInputStream contains all of the following methods except: (a) readLine() (b) readChar() (c) readObject() (d) readInt()

 True/False

  1. A stream is an object that allows for the flow of data between your program and some I/O device or some file.
  2. Every input file and every output file used by your program has only one name which is the same name used by the operating system.
  3. The FileNotFoundException is a descendant of the class IOException.
  4. When your program is finished writing to a file, it should close the stream connected to that file.
  1. Only the classes provided for file output contain a method named close.
  2. The methods of the scanner class do not behave the same when reading from a text file as they do when used to read from the keyboard.
  3. Using BufferedReader to read integers from a file requires the String input to be parsed to an integer type.
  4. A full path name gives a complete path name, starting from the directory the program is in.
  5. The File class contains methods that allow you to check various properties of a file.

 Short Answer/Essay

Explain the differences between a text file, an ASCII file and a binary file. Write a Java statement to create and open an output stream to a file named autos.txt. Explain what happens when an output file is opened in Java. Write a Java method that returns a String representing a file name entered by the user. Use the output stream to the file autos.txt created above in number 2 to write the line “Mercedes” to the file. What happens when the method close is invoked on a stream? Create try and catch block that opens a file named statistics.txt for output. Writes the integers 24, 55, and 76 to the file, and then closes the file. Answer: Answer: Write a Java statement that creates an output stream to append data to a file named autos.txt. Answer: Write a Java statement to create an input stream to a file named autos.txt. Answer: Write a complete Java program using a Scanner object that opens a file named autos.txt and displays each line to the screen.