Question 149 / 297:  What is the result of compiling and running the following code?
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

class Base {

int code = 99;

Base() {

code = 77;

}

}

public class Derived extends Base implements Serializable {

public static void main(String[] args) {

Derived derived1 = new Derived();

derived1.code = 88;

try {

FileOutputStream fos = new FileOutputStream("data.txt");

ObjectOutputStream os = new ObjectOutputStream(fos);

os.writeObject(derived1);

os.close();

FileInputStream fis = new FileInputStream("data.txt");

ObjectInputStream is = new ObjectInputStream(fis);

Derived derived2 = (Derived) is.readObject();

System.out.print(derived2.code);

is.close();

} catch (Exception x) {

}

}

}

A  0
B  77
C  88
D  99
E  Compilation error
<< First < Previous Next > Last >>
Explanation:

When super class is not serialized, then when deserializing the instance of the sub class, the constructor of the super is called.

Exam Home Page
Ask
Lakeima Roberts
Start Quiz
Saylor Foundation
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>