Menu
Topics Index
...
`

Store Hemanths Data2124


If you need explanation Read this topic

If you need Answer Take test on this topic

Store Hemanths Data2124
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 = 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.

Ans is C..Compilation error Line b...we are reaading into hemanthRestored need to Student typ bt here it is giving Object..so error

Posted by Uday Kumar    2014-06-27 13:11:09


ans c. studnt hemnthrestored= ois.readobjct() tis line needs type casting.
we need to create interface externalizabletest.
compilatn err

Posted by Maheshwari Natarajan    2014-06-27 13:16:02


c

Posted by Sravyareddy Kondaveeti    2014-06-27 13:45:18


Answer c.
compilation err.

line b needs typecasting - studnt type.

class student needs externalizableTest interface.

or replace extends keyword instead of interface.

Posted by NagaRaj Nataraj    2014-06-28 07:55:27


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

Posted by Merit Campus    2014-06-30 05:15:35


© meritcampus 2019

All Rights Reserved.

Open In App