Menu
Topics Index
...
`

Store Hemanths Data


If you need explanation Read this topic

If you need Answer Take test on this topic

Store Hemanths Data
What will be the output of the following program?
import java.io.*;
public class ExternalizableTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);
        Student hemanth = new Student();
        hemanth.name = "Hemanth"; hemanth.rollNumber = 25; hemanth.section = 'C';
        System.out.println("Before - " + hemanth.name + " " + hemanth.rollNumber + " " + hemanth.section);
        oos.writeObject(hemanth); // LINE A
        oos.close();
        byte[] objectDataBytes = byteArrayOutputStream.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(objectDataBytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        Student hemanthRestored = (Student) ois.readObject(); // LINE B
        System.out.println("After - " + hemanthRestored.name + " " + hemanthRestored.rollNumber + " " + hemanthRestored.section);
        ois.close();
    }
}
class Student implements Externalizable {
    String name = "";  int rollNumber = 0;    char section = 'A';
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = in.readUTF();
        rollNumber = in.readInt();
    }
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(name);
        out.writeInt(rollNumber);
    }
}

A. Before - Hemanth 25 C
After - Hemanth 25 A
B. Before - Hemanth 25 C
After - Hemanth 25 C
C. Compilation Error
D. Runtime Error at LINE A
E. Runtime Error at LINE B
Topic: Java Serialization Process -

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer : E
at line B we are typeCasting the hemanthRestored object  to access the Student class but we are not providing a proper constructor ..

Posted by Deepak Kumar Yadav    2014-05-23 15:54:49


Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-26 12:43:31


© meritcampus 2019

All Rights Reserved.

Open In App