Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 11. Files and Streams > Review Questions

Review Questions

11.22How many methods are defined in the Serializable interface?

Select the one correct answer.

  1. None

  2. One

  3. Two

  4. Three

  5. None of the above.

11.23Which of the following best describes the data written by an ObjectOutputStream?

Select the one correct answer.

  1. Bytes and other Java primitive types.

  2. Object hierarchies.

  3. Object hierarchies and Java primitive types.

  4. Single objects.

  5. Single objects and Java primitive types.

11.24Given the following code:
public class Person  {
  protected String name;
  Person() { }
  Person(String name) { this.name = name; }
}
________________________________________________________________________
import java.io.Serializable;
public class Student extends Person implements Serializable {
  private long studNum;
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }
  public String toString() { return "(" + name + ", " + studNum + ")"; }
}
________________________________________________________________________
import java.io.*;
public class RQ800_10 {
  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    Student stud1 = new Student("Aesop", 100);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    Student stud2 = (Student) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100)(Aesop, 100).

  4. It prints (Aesop, 100)(null, 100).

  5. It prints (Aesop, 100)( , 100).

11.25Given the following code:
public class Person  {
  protected String name;
  Person() { this.name = "NoName"; }
  Person(String name) { this.name = name; }
}
_______________________________________________________________________
import java.io.Serializable;
public class Student extends Person implements Serializable {
  private long studNum;
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }
  public String toString() { return "(" + name + ", " + studNum + ")"; }
}
________________________________________________________________________
import java.io.*;
public class RQ800_20 {

  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    Student stud1 = new Student("Aesop", 100);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    Student stud2 = (Student) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100)(Aesop, 100).

  4. It prints (Aesop, 100)(null, 100).

  5. It prints (Aesop, 100)(NoName, 100).

11.26Given the following code:
import java.io.Serializable;
public class Person implements Serializable {
  protected String name;
  Person() { this.name = "NoName"; }
  Person(String name) { this.name = name; }
}
________________________________________________________________________
public class Student extends Person {
  private long studNum;
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }
  public String toString() { return "(" + name + ", " + studNum + ")"; }
}
________________________________________________________________________
import java.io.*;
public class RQ800_30 {

  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    Student stud1 = new Student("Aesop", 100);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    Student stud2 = (Student) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100)(Aesop, 100).

  4. It prints (Aesop, 100)(null, 100).

  5. It prints (Aesop, 100)(NoName, 100).

11.27Given the following code:
import java.io.Serializable;
public class Person implements Serializable {
  protected transient String name;
  Person(String name) { this.name = name; }
}
________________________________________________________________________
public class Student extends Person {
  private long studNum;
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }
  public String toString() { return "(" + name + ", " + studNum + ")"; }
}
________________________________________________________________________
import java.io.*;
public class RQ800_40 {

  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    Student stud1 = new Student("Aesop", 100);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    Student stud2 = (Student) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100)(Aesop, 100).

  4. It prints (Aesop, 100)(null, 100).

  5. It prints (Aesop, 100)(NoName, 100).

11.28Given the following code:
import java.io.Serializable;
public class Person implements Serializable {
  protected transient String name;
  Person() { this.name = "NoName"; }
  Person(String name) { this.name = name; }
}
________________________________________________________________________
import java.io.*;
public class Student extends Person {
  private long studNum;
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }

  public String toString() { return "(" + name + ", " + studNum + ")"; }

  private void writeObject(ObjectOutputStream oos) throws IOException {
      oos.defaultWriteObject();
      oos.writeObject("NewName");
  }

  private void readObject(ObjectInputStream ois)
               throws IOException, ClassNotFoundException {
      ois.defaultReadObject();
      name = (String) ois.readObject();
  }
}
________________________________________________________________________
import java.io.*;
public class RQ800_50 {

  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    Student stud1 = new Student("Aesop", 100);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    Student stud2 = (Student) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100)(Aesop, 100).

  4. It prints (Aesop, 100)(NewName, 100).

  5. It prints (Aesop, 100)(NoName, 100).

11.29Given the following code:
public class Person {
  protected transient String name;
  Person() { this.name = "NoName"; }
  Person(String name) { this.name = name; }
}
________________________________________________________________________
public class Student extends Person {
  protected long studNum;
  Student() { }
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
  }
}
________________________________________________________________________
import java.io.*;

public class GraduateStudent extends Student implements Serializable {
  private int year;
  GraduateStudent(String name, long studNum, int year) {
    super(name, studNum);
    this.year = year;
  }

  public String toString() {
    return "(" + name + ", " + studNum + ", " + year  + ")";
  }

  private void readObject(ObjectInputStream ois)
               throws IOException, ClassNotFoundException {
      ois.defaultReadObject();
      name = "NewName";
      studNum = 200;
      year =2;
  }
}
________________________________________________________________________
import java.io.*;
public class RQ800_70 {

  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    GraduateStudent stud1 = new GraduateStudent("Aesop", 100, 1);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    GraduateStudent stud2 = (GraduateStudent) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100, 1)(Aesop, 100, 1).

  4. It prints (Aesop, 100, 1)(NewName, 0, 1).

  5. It prints (Aesop, 100, 1)(NewName, 200, 2).

11.30Given the following code:
import java.io.Serializable;
public class Person implements Serializable {
  protected transient String name;
  Person(String name) { this.name = name; }
}
_______________________________________________________________
public class Student extends Person {
  private static int numOfStudents;
  private long studNum;
  Student(String name, long studNum) {
    super(name);
    this.studNum = studNum;
    ++numOfStudents;
  }
  public String toString() {
    return "(" + name + ", " + studNum + ", " + numOfStudents + ")";
  }
}
________________________________________________________________
import java.io.*;
public class RQ800_80 {
  public static void main(String args[])
                     throws IOException, ClassNotFoundException {
    FileOutputStream outputFile = new FileOutputStream("storage.dat");
    ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
    Student stud1 = new Student("Aesop", 100);
    System.out.print(stud1);
    outputStream.writeObject(stud1);
    outputStream.flush();
    outputStream.close();

    Student student = new Student("Mowgli", 300);

    FileInputStream inputFile = new FileInputStream("storage.dat");
    ObjectInputStream inputStream = new ObjectInputStream(inputFile);
    Student stud2 = (Student) inputStream.readObject();
    System.out.println(stud2);
    inputStream.close();
  }
}

					  

Which statement about the program is true?

Select the one correct answer.

  1. It fails to compile.

  2. It compiles, but throws an exception at runtime.

  3. It prints (Aesop, 100, 1)(Aesop, 100, 1).

  4. It prints (Aesop, 100, 1)(null, 100, 2).

  5. It prints (Aesop, 100, 1)(null, 100, 1).


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial