Menu
Topics Index
...
`

Exception Handling by using trycatch blocks1650


If you need explanation Read this topic

If you need Answer Take test on this topic

Exception Handling by using trycatch blocks1650
What will be the output of the following program?
import java.io.*;
class SuperClass implements Serializable {
    protected int salary;
    protected SubClass subClass;
    public SuperClass(SubClass subClass, int salary) {
        this.subClass = subClass;
        this.salary = salary;
        subClass.value = salary;
    }
    public SubClass getSubClass() { return subClass;  }
}
class SubClass {
    int value;
    public SubClass(int value) { this.value = value; }
    public int getValue() { return value;  }
}
public class ByteStream {
    public static void main(String arg[]) throws IOException {
        SubClass subClass = new SubClass(2500);
        SuperClass superClass = new SuperClass(subClass, 5000);
        System.out.println(superClass.getSubClass().getValue());
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("D:/hello.txt"));
            outputStream.writeObject(superClass); // LINE A
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) { e.printStackTrace(); }
        try {
            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("D:/hello.txt"));
            superClass = (SuperClass) inputStream.readObject(); // LINE B
            inputStream.close();
            System.out.println(superClass.getSubClass().getValue());
        } catch (Exception e) { e.printStackTrace(); }
    }
}

A. 2500
2500
B. 5000
5000
C. 2500
5000
D. Some other output
E. Compilation Error or 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.

D.is ans such as 5000 and then an exception araises if the hello.txt is not found in d drive.

Posted by Shabaaz Shaikz    2014-02-19 12:56:55


For a class to be serializable, it must implement the java.io.Serializable interface and all of the fields in the class must be serializable. We can indicate properties that shouldn't be serialized as "transient". This program tries to write the state of SuperClass object to a file named "hello.txt". But subClass, one of the members of SuperClass is not serializable (SubClass doesn't implement the Serializable interface here). Hence, JVM will report that SubClass is not serializable. The output is 5000 (by first SOP statement in main) followed by an exception from JVM indicating that SubClass is not serializable. Answer is D.

Posted by Shaileshwar Sharma    2014-02-19 15:01:16


The correct answer is E, because it throws a Not Serializable Exception. Since Shaileshwar Sharma explantion is correct, he will get the recharge.

Posted by Merit Campus    2014-02-20 04:45:59


When there is ambiguity between Runtime Error and Some other output + Runtime Error, please choose Runtime Error.

Posted by Merit Campus    2014-02-20 04:47:03


Shaileshwar Sharma we will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-20 04:47:35


© meritcampus 2019

All Rights Reserved.

Open In App