Menu
Topics Index
...
`

Files : Creating ObjectInputStream and ObjectOutputStream


If you need explanation Read this topic

If you need Answer Take test on this topic

Files : Creating ObjectInputStream and ObjectOutputStream
What will be the output of the following program?
import java.io.*;
public class SaveSecrets {

    public static void main(String[] args) {
        User user = new User("My User Name", "My Password");
        try {
            ObjectOutput out = new ObjectOutputStream(new FileOutputStream("secrets.txt"));
            user.writeExternal(out);
            out.close();
            ObjectInput in = new ObjectInputStream(new FileInputStream("secrets.txt"));
            user.readExternal(in);
            in.close();
        } catch (Exception e) {    }
        System.out.println(user);
    }
}

class User implements Externalizable {
    private String userName;
    private transient String password;
    public User(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        String line = in.readUTF();
        userName = line.split("~")[0];
        password = line.split("~")[0];
    }
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(userName + "~" + password);
    }
    public String toString() {
        return userName + " " + password;
    }
}

A. My User Name My Password
B. My User Name null
C. null My Password
D. Some other output not shown here
E. Compilation 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 d, in readExternal method for userName,password same value is passed line.split("~")[0] (i.e My User Name) so the o/p will be " My User Name My User Name"

Posted by Sulekha Metta    2013-08-20 15:20:41


Congratulations Sulekha Metta. You are this dose winner. Please send us your mobile details in a private message and we will process the recharge asap.

Posted by Merit Campus    2013-08-21 04:07:38


thanks mc..received rc :)

Posted by Sulekha Metta    2013-08-21 15:10:33


© meritcampus 2019

All Rights Reserved.

Open In App