Menu
Topics Index
...
`

Files1919


If you need explanation Read this topic

If you need Answer Take test on this topic

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

public class ObjProgram {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("abc.txt");
        ObjectInputStream in = new ObjectInputStream(fis);
        MyBean sc2 = new MyBean("Test1", "Test2");
        in = new ObjectInputStream(fis);
        System.out.println(sc2);
    }
}

class MyBean implements Serializable {
    private final String a;
    private transient String b;
    public MyBean(String aa, String bb) {
        a = "Merit Campus: " + aa;
        b = "Program: " + bb;
    }
    public String toString() {
        return a + "\n" + b;
    }
}

A. Merit Campus: Test1
Program: Test2
B. Merit Campus: Test2
Program: Test1
C. Merit Campus: Test1
Program: null
D. Merit Campus: null
Program: Test2
E. Some other output
F. 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 F. File not found exception arises and if a file is created then EOFException arises since there is a null stream in ObjectInputStream

Posted by Nikhil Sulibhavi    2014-05-06 12:45:02


Answer is F RunTime Error arises because abc.txt file is not found during runtime execution.

Posted by Asad Ahmed    2014-05-06 16:01:03


Answer is F. FileInputStream throws FileNotFoundException if it cannot find the file specified, when trying open a connection to the file, which is the case here. If abc.txt does exist and is empty, then ObjectInputStream throws EOFException because there is nothing to read. If abc.txt doesn't contain properly serialized data, then ObjectInputStream throws StreamCorruptedException. [If MyBean was previously written (serialized) to the file using ObjectOutputStream, then deserializing it using ObjectInputStream and  corresponding methods would output <<Merit Campus: Test1 null>> (b would have the default value null since transient variables are not serializable)]

Posted by Shaileshwar Sharma    2014-05-07 03:01:13


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-08 04:04:24


© meritcampus 2019

All Rights Reserved.

Open In App