Developed by Y. Daniel Liang using Java Servlets (Current time is Sat Nov 21 19:29:48 EST 2009)

The questions are to accompany Introduction to Java Programming, 4E by Y. Daniel Liang. Please report errors to y.daniel.liang@gmail.com.

Introduction to Java Programming, 4E has been superseded by Introduction to Java Programming, 5E.

Chapter 17: Input and Output

Section 17.2 Stream Classes
1   InputStream and OutputSteam are __________.

A. The top-level abstract classes defined in the java.io package, from which all other stream classes inherit.
B. The top-level abstract classes defined in the java.io package, from which all other byte stream classes inherit.
C. Classes that you can instantiate to read and write bytes.
D. Interfaces that define methods that can be used to read and write bytes.

2   Which method can you use to find out the number of the bytes in a file using InputStream?

A. length()
B. available()
C. size()
D. getSize()

3   Which of the following is not a valid method for an instance of Reader?

A. read()
B. read(char[])
C. skip(long)
D. close()
E. available()

Section 17.3 The File Class
4   What are the reason to create an instance of the File class?

A. To determine whether the file exist.
B. To obtain the properties of the file such as whether the file can be read, written, or is hidden.
C. To rename the file.
D. To delete the file.
E. All of the above.

5   Which of the following returns the path separator character?

A. File.pathSeparator
B. File.pathSeparatorChar
C. File.separator
D. File.separatorChar
E. None of the above.

6   Which of the following statements creates an instance of File on Window for the file c:\t.txt?

A. new File("c:\txt.txt")
B. new File("c:\\txt.txt")
C. new File("c:/txt.txt")
D. new File("c://txt.txt")

7   Which of the following statements are true?

A. If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") returns null.
B. If a directory (e.g., c:\liang) does not exist, new File("c:\liang") returns null.
C. If a file (e.g., c:\temp.txt) does not exist, new File("c:\\temp.txt") creates a new file named c:\temp.txt.
D. If a directory (e.g., c:\liang) does not exist, new File("c:\liang") creates a new directory named c:\liang.
E. None of the above.

Section 17.4 Process External Files
8   What does the following code do?

FileInputStream fis = new FileInputStream("test.dat");

A. It creates a new file named test.dat if it does not exist and opens the file so you can write to it.
B. It creates a new file named test.dat if it does not exist and opens the file so you can write to it and read from it.
C. It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it.
D. It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it and read from it.
E. None of the above is correct.

9   Which type of exception occurs when creating a FileInputStream for a nonexistent file?

A. FileNotExist
B. FileNotExistException
C. FileNotFound
D. FileNotFoundException

10   If you use FileWriter to write a character to a file, how many bytes are stored for the character?

A. 1
B. 2
C. 3
D. Depends on the character encoding scheme

Section 17.6 Data Streams
11   Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?

A. DataOutputStream outfile = new DataOutputStream(new File("out.dat"));
B. DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat"));
C. DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat"));
D. DataOutputStream outfile = new DataOutputStream("out.dat");

12   After the following program is finished, how many btyes are written to the file t.dat?

import java.io.*;

public class Test {
   public static void main(String[] args) throws IOException {
     DataOutputStream output = new DataOutputStream(
       new FileOutputStream("t.dat"));
     output.writeShort(1234);
     output.writeShort(5678);
     output.close();
   }
}

A. 2 bytes.
B. 4 bytes.
C. 8 bytes.
D. none of the above.

Section 17.8 Buffered Streams
13   Which of the following statements is correct to create a BufferedWriter stream to write to a file named out.dat?

A. BufferedWriter bw = new BufferedWriter(new File("out.dat"));
B. BufferedWriter bw = new BufferedWriter(new FileWriter("out.dat"));
C. BufferedWriter bw = new BufferedWriter("out.dat");
D. BufferedWriter bw = new BufferedWriter(new FileOutputStream("out.dat"));

Section 17.11 Object Streams
14   The Loan class given in the text does not implement java.io.Serializable. Analize the following code.

public class Foo implements java.io.Serializable {
   private int v1;
   private static double v2;
   private Loan v3 = new Loan();
}

A. An instance of Foo can be serialized because Foo implements Serializable.
B. An instance of Foo cannot be serialized because Foo contains a non-serializable instance variable v3.
C. If you mark v3 as transient, an instance of Foo is serializable.
D. b and c

Section 17.12 Random Access Files
15   To create a file, you can use __________.

A. FileOutputStream
B. FileWriter
C. RandomAccessFile
D. All of the above.

16   Which of the following is the legal mode for creating a new RandomAccessFile stream?

A. "w"
B. 'r'
C. "rw"
D. "rwx"

17   What happens if the file test.dat does not exist when you attempt to compile and run the following code?

import java.io.*;

class Test {
   public static void main(String[] args) {
     try {
       RandomAccessFile raf =
         new RandomAccessFile("test.dat", "r");
       int i = raf.readInt();
     }
     catch(IOException ex) {
       System.out.println("runtime exception");
     }
}

A. The program does not compile because raf is not created correctly.
B. The program does not compile because readInt() is not implemented in RandomAccessFile.
C. The program compiles, but throws IOException because the file test.dat doesn't exist. The program displays runtime exception.
D. The program compiles and runs fine, but nothing is displayed on the console.

18   With which I/O class can you append or update a file?


A. RandomAccessFile(),
C. DataOutputStream()
D. None of the above

19   Which of the following statements is not true?


A. A static variable is not serialized.
B. A transient variable is not serialized.
C. An object must be an instance of Serializable or Externalizable for it to be serialized.
D. The methods in an object are serialized.
E. All of the above are true.

Section 17.13 Parsing Text Files (Optional)
20   Which of the following statements is incorrect to create a StreamTokenizer stream to read a file named in.dat?

A. StreamTokenizer st = new StreamTokenizer(new FileInputStream("in.dat"));
B. StreamTokenizer st = new StreamTokenizer(new FileInputStream(new File("in.dat")));
C. StreamTokenizer st = new StreamTokenizer(new FileReader(new File("in.dat")));
D. StreamTokenizer st = new StreamTokenizer(new FileReader("in.dat"));
E. All above, but a and b are deprecated since JDK1.1.

21   Which of the following methods cannot be invoked by an instance of StreamTokenizer?
A. nextToken()
B. toString()
C. equals()
D. close()