Java OCA OCP Practice Question 2063

Question

Given 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 Main {

 public static void main(String args[])
                      throws IOException, ClassNotFoundException {
   FileOutputStream outputFile = new FileOutputStream("storage.dat");
   ObjectOutputStream outputStream = new ObjectOutputStream(outputFile);
   Student stud1 = new Student("Java", 100);
   System.out.print(stud1);/*from  www.j av a  2s . com*/
   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.

 (a) It fails to compile.
 (b) It compiles, but throws an exception at runtime.
 (c) It prints (Java, 100)(Java, 100).
 (d) It prints (Java, 100)(null, 100).
 (e) It prints (Java, 100)(NoName, 100).


(d)

Note

The class Student is Serializable, because its superclass Person is Serializable.

During deserialization, the default constructor of the superclass Person is not called because the superclass is Serializable.

But the field name is transient, and therefore not serialized.




PreviousNext

Related