Menu
Question Index
...

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';
    class Student() {}
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = in.readUTF();
        rollNumber = in.readInt();
        section = in.readChar();
    }
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(name);
        out.writeInt(rollNumber);
        out.writeChar(section);
    }
}


Before - Hemanth 25 C
After - Hemanth 25 A
Before - Hemanth 25 C
After - Hemanth 25 C
Compilation Error
Runtime Error at LINE A
Runtime Error at LINE B

Doubts

Problems

Topic: Java Serialization Process -

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App