In order for an object to be serialized, its class must implement the ________ interface.

Introduction to Java Programming, Includes Data Structures, Eleventh Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Chapter 17 Binary I/O


Section 17.2 How is I/O Handled in Java?

17.1  Which of the following statements are true?

A. A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing.

B. You can use the PrintWriter class for outputting text to a file.

C. You can use the Scanner class for reading text from a file.

D. An input object is also called an input stream.

E. An output object is also called an output stream.


Section 17.3 Text I/O vs. Binary I/O

17.2  Which of the following statements are true?

A. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.

B. Text I/O involves encoding and decoding.

C. Binary I/O does not require conversions.

D. Binary I/O is more efficient than text I/O, because binary I/O does not require encoding and decoding.

E. Binary files are independent of the encoding scheme on the host machine and thus are portable.


Section 17.4 Binary I/O Classes

17.3  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()


17.4  Which of the following statements are true?

A. All methods in FileInputStream/FileOutputStream are inherited from InputStream/OutputStream.

B. You can create a FileInputStream/FileOutputStream from a File object or a file name using FileInputStream/FileOutputStream constructors.

C. The return value -1 from the read() method signifies the end of file.

D. A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file.

E. A java.io.FileNotFoundException would occur if you attempt to create a FileOutputStream with a nonexistent file.


17.5  To append data to an existing file, use _____________ to construct a FileOutputStream for file out.dat.

A. new FileOutputStream("out.dat")

B. new FileOutputStream("out.dat", false)

C. new FileOutputStream("out.dat", true)

D. new FileOutputStream(true, "out.dat")


17.6  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. It creates a FileInputStream for test.dat if test.dat exists.


17.7  Which type of exception occurs when creating a DataInputStream for a nonexistent file?

A. FileNotExist

B. FileNotExistException

C. FileNotFound

D. FileNotFoundException


17.8  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");


17.9  After the following program is finished, how many bytes 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. 16 bytes.


17.10  After the following program is finished, how many bytes 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.writeChar('A');
    output.close();
  }
}

A. 2 bytes.

B. 4 bytes.

C. 8 bytes.

D. none of the above.


Section 17.5 Case Study: Copying Files

17.11  After the following program is finished, how many bytes 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.writeChars("ABCD");
    output.close();
  }
}

A. 2 bytes.

B. 4 bytes.

C. 8 bytes.

D. 12 bytes.

E. 16 bytes.


17.12  After the following program is finished, how many bytes 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.writeUTFString("ABCD");
    output.close();
  }
}

A. 2 bytes.

B. 4 bytes.

C. 6 bytes.

D. 8 bytes.

E. 10 bytes.


17.13  Which of the following statements are true?

A. All files are stored in binary format. So, all files are essentially binary files.

B. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.

C. Encoding and decoding are automatically performed by text I/O.

D. For binary input, you need to know exactly how data were written in order to read them in correct type and order.


Section 17.6 Object I/O

17.14  Which of the following statements are true?

A. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition for primitive type values and strings.

B. Since ObjectInputStream/ObjectOutputStream contains all the functions of DataInputStream/DataOutputStream, you can replace DataInputStream/DataOutputStream completely by ObjectInputStream/ObjectOutputStream.

C. To write an object, the object must be serializable.

D. The Serializable interface does not contain any methods. So it is a mark interface.

E. If all the elements in an array is serializable, the array is serializable too.


17.15  The Loan class given in the text does not implement java.io.Serializable. Analyze 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.


17.16  Which of the following statements is true?

A. A static variable is not serialized.

B. A transient variable is not serialized.

C. An object must be an instance of Serializable for it to be serialized.

D. The methods in an object are serialized.


Section 17.7 Random Access Files

17.17  To create a file, you can use __________.

A. FileOutputStream

B. FileWriter

C. RandomAccessFile

D. All of the above


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

A. "w"

B. 'r'

C. "rw"

D. "rwx"


17.19  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("IO 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 IO exception.

D. The program compiles and runs fine, but nothing is displayed on the console.


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

A. RandomAccessFile()

B. OutputStream()

C. DataOutputStream()

D. None of the above