Apply Your Knowledge

Review Questions

1:What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
2:What is an I/O filter?
3:What is the purpose of the File class?
4:What interface must an object implement before it can be written to a stream as an object?
5:What is the difference between the File and RandomAccessFile classes?
6:Which class allows you to read objects directly from a stream?
7:What value does read() return when it has reached the end of a file?
8:What value does readLine() return when it has reached the end of a file?
9:How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?
10:What is your platform's default character encoding?

Exam Questions

1:Which of the following is true?
  1. The InputStream and OutputStream classes are byte oriented.

  2. The ObjectInputStream and ObjectOutputStream classes do not support serialized object input and output.

  3. The Reader and Writer classes are character-oriented.

  4. The Reader and Writer classes are the preferred solution to serialized object output.

2:How many bytes does the following program write to temp.txt?
import java.io.*;

public class TestIOApp {
 public static void main(String args[]) throws IOException {
  FileOutputStream outStream = new FileOutputStream("test.txt");
  String s = "test";
  for(int i=0;i<s.length();++i)
   outStream.write(s.charAt(i));
  outStream.close();
 }
}

  1. 2 bytes

  2. 4 bytes

  3. 8 bytes

  4. 16 bytes

3:Which of the following is true about I/O filters?
  1. Filters are supported on input, but not on output.

  2. Filters are supported by the InputStream/OutputStream class hierarchy, but not by the Reader/Writer class hierarchy.

  3. Filters read from one stream and write to another.

  4. A filter can alter data that is read from one stream and written to another.

4:Which of the following is true?
  1. Any Unicode character is represented using 16 bits.

  2. Seven bits are needed to represent any ASCII character.

  3. UTF-8 characters are represented using only eight bits.

  4. UTF-16 characters are represented using only 16 bits.

5:Which of the following is true?
  1. The Serializable interface is used to identify objects that can be written to an output stream.

  2. The Externalizable interface is implemented by classes that control the way in which their objects are serialized.

  3. The Serializable interface extends the Externalizable interface.

  4. The Externalizable interface extends the Serializable interface.

6:Which of the following is true about the File class?
  1. A File object can be used to change the current working directory.

  2. A File object can be used to access the files in the current working directory.

  3. When a File object is created, a corresponding directory or file is created in the local file system.

  4. File objects are used to access files and directories on the local file system.

  5. File objects can be garbage collected.

  6. When a File object is garbage collected, the corresponding file or directory is deleted.

7:What output does the following program display?
import java.io.*;

public class TestIOApp {
 public static void main(String args[]) throws IOException {
  StringReader stringin = new StringReader("test");
  LineNumberReader in = new LineNumberReader(stringin);
  PrintWriter out = new PrintWriter(System.out);
  out.println(in.readLine());
  out.flush();
 }
}

  1. test

  2. 1. test

  3. 1: test

  4. 1 test

8:What output does the following program display?
import java.io.*;

public class TestIOApp {
 public static void main(String args[]) throws IOException {
  RandomAccessFile file = new RandomAccessFile("test.txt","rw");
  file.writeBoolean(true);
  file.writeInt(123456);
  file.writeInt(7890);
  file.writeLong(1000000);
  file.writeInt(777);
  file.writeFloat(.0001f);
  file.seek(5);
  System.out.println(file.readInt());
  file.close();
 }
}

  1. 123456

  2. 7890

  3. 1000000

  4. 777

  5. .0001

9:How do you create a Reader object from an InputStream object?
  1. Use the static createReader() method of the InputStream class.

  2. Use the static createReader() method of the Reader class.

  3. Create an InputStreamReader object, passing the InputStream object as an argument to the InputStreamReader constructor.

  4. Create an OutputStreamReader object, passing the InputStream object as an argument to the OutputStreamReader constructor.

10:Which of the following is true?
  1. Writer classes can be used to write characters to output streams using different character encodings.

  2. Writer classes can be used to write Unicode characters to output streams?

  3. Writer classes have methods that support the writing of values of any Java primitive type to output streams.

  4. Writer classes have methods that support the writing of objects to output streams.

Answers to Review Questions

A1: The Reader/Writer class hierarchy is character oriented, and the InputStream/OutputStream class hierarchy is byte oriented. See the section “Streams.”
A2: An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another. See the section “Filtered I/O.”
A3: The File class is used to create objects that provide access to the files and directories of a local file system. See the section “The File Class.”
A4: An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object. See the section “Object I/O.”
A5: The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods that are needed to directly access data contained in any part of a file. See the sections “The File Class,” and “The RandomAccessFile Class.”
A6: The ObjectInputStream class supports the reading of objects from input streams. See the section “Object I/O.”
A7: The read() method returns -1 when it has reached the end of a file. See the section “The read() Method.”
A8: The readLine() method returns null when it has reached the end of a file. See the section “Buffered Character I/O.”
A9: Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters that use 8-, 16-, and 18-bit patterns. UTF-16 uses 16-bit and larger bit patterns. See the section “Character Sets and Codings.”
A10: If you are running Java on English Windows platforms, it is probably Cp1252. If you are running Java on English Solaris platforms, it is most likely 8859_1. See the section “Character Sets and Codings.”

Answers to Exam Questions

A1: A. and C. The ObjectInputStream and ObjectOutputStream classes are the preferred classes for performing serialized object I/O. See the section “Object I/O.”
A2: B. The "test" string is written as 4 bytes. See the section “Character Sets and Codings.”
A3: C. and D. Filters are supported on both input and output and by the InputStream/OutputStream and Reader/Writer class hierarchies. See the section “Filtered I/O.”
A4: A. and B. UTF-8 and UTF-16 are multi-byte formats of variable length. See the section “Character Sets and Codings.”
A5: A., B., and D. Externalizable extends Serializable. See the section “Object I/O.”
A6: B., D., and E. File does not provide methods to change the current working directory. The creation of a File object does not result in the creation of a corresponding directory or file in the local file system. The garbage collection of a File object does not normally affect the local file system. See the section “The File Class.”
A7: A. LineNumberReader does not add line numbers to the input content. It makes them available through its getLineNumber() method. See the section “The LineNumberReader Class.”
A8: B. The number 7890 is stored at file location 5 because the previously written boolean and int values occupy 5 bytes. See the sections “The RandomAccessFile,” and “Data I/O.”
A9: C. The InputStreamReader class provides a constructor for creating Reader objects from InputStream objects. See the section “The InputStreamReader Class.”
A10: A. and B. Writer classes are character-oriented and do not support other primitive types or objects. See the section “The Reader and Writer Classes.”
Suggested Readings and Resources

1. The JDK 1.3 java.io package API description. You can download this or browse it online at http://java.sun.com/j2se/1.3/docs.html.

2. Sun's tutorial at http://java.sun.com/docs/books/tutorial.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.96.247