Menu
Topics Index
...
`

Files2020


If you need explanation Read this topic

If you need Answer Take test on this topic

Files2020
What will be the output of the following program?
import java.io.*;

public class ExcuseMePlease {
    public static void main(String[] args) throws Exception {
        File file = File.createTempFile("excuse", "me");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        Excuse excuse = new Excuse();
        excuse.please.k = 50;
        oos.writeObject(excuse);
        oos.close();
        excuse.j = 75;
        excuse.please.k = 25;
        excuse.me = new Me();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        excuse = (Excuse) ois.readObject();
        ois.close();
        System.out.println(excuse.j + " " + excuse.me + " " + excuse.please.k);
    }
}
class Excuse implements Serializable {
    int j = 25;
    Me me = null;
    Please please = new Please();
}
class Me {
    int i;
}
class Please implements Serializable {
    transient int k;
}

A. 25 null 0
B. 25 null 25
C. 75 null 0
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java ObjectInputStream

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 A. excuse.j is initialized to 25 when the object is invoked. Next null is printed since excuse.me is initialised to null upon object creation. As the variable k in Please class is transient it is initialised to default 0 and is not serialized.

Posted by Nikhil Sulibhavi    2014-05-08 13:31:08


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

Posted by Merit Campus    2014-05-09 04:00:50


© meritcampus 2019

All Rights Reserved.

Open In App