Java OCA OCP Practice Question 2057

Question

Given 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);/*  w  ww . j ava  2 s.c  om*/
    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   w  w w.ja v a  2  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)( , 100).


(d)

Note

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




PreviousNext

Related