Java OCA OCP Practice Question 2061

Question

Given 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 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. j a v  a 2s. 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).


(c)

Note

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




PreviousNext

Related