Java OCA OCP Practice Question 2059

Question

Given 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 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);/* w  w  w. j a v  a2 s .  c  om*/
    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).


(e)

Note

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

The default constructor initializes the field name to the string NoName.




PreviousNext

Related