Menu
Topics Index
...
`

ABC Serialization


If you need explanation Read this topic

If you need Answer Take test on this topic

ABC Serialization
What will be the output of the following program?
import java.io.*;
public class Serialization {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        B bin = new B();
        bin.a = 45; bin.b = 21; bin.b2 = 90; bin.co.c = 31;
        FileOutputStream fos = new FileOutputStream("test.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(bin);
        oos.close();
        FileInputStream fis = new FileInputStream("test.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        B bout = (B) ois.readObject();
        System.out.println(bout.a + " " + bout.b + " " + bout.co.c + " " + bout.b2);
    }
}

class A { int a; }

class B extends A implements Serializable {
    C co = new C();
    int b;
    transient int b2;
}

class C implements Serializable { int c; }

A. 0 21 31 0
B. 45 21 31 0
C. 45 21 31 90
D. Compilation Error
E. Runtime Error
Topic: Unknown

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-B

Posted by Kick Bhasha    2014-01-31 13:25:16


ans D :  A compilation error occurs b'coz of implementing serializable

Posted by Karthik Narahari    2014-01-31 13:35:53


Ans is A since class A is not serialized so its instance variable 'a' is not participate in serialization. Similarly instance variable 'b2' of class B is marked as transient so it also not participate in serialization.  Java says that the variable is serialized if and only if the class implements serializable interface and do not contain transient and static.

Posted by Mohd Khayam Uddin    2014-01-31 13:36:51


ans "a"

Posted by Rolly Singh    2014-01-31 15:26:55


Class A is not serialised,so ans is 'A'

Posted by Kailash Singh    2014-01-31 16:39:09


Congratulations Mohd Khayam Uddin. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-03 04:36:36


© meritcampus 2019

All Rights Reserved.

Open In App