Menu
Question Index
...

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(); }
    }
}


2500
2500
5000
5000
2500
5000
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App