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? |
1: | Which of the following is true?
|
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(); } }
|
3: | Which of the following is true about I/O filters?
|
4: | Which of the following is true?
|
5: | Which of the following is true?
|
6: | Which of the following is true about the File class?
|
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(); } }
|
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(); } }
|
9: | How do you create a Reader object from an InputStream object?
|
10: | Which of the following is true?
|
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.” |
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.” |
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.
13.59.96.247