Menu
Question Index
...

What is the output of the following program?

import java.io.*;
public class StoreObjectState {
    public static void main(String arg[]) {
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("file.txt"));
            outputStream.writeObject(new Circle(20));
            outputStream.writeObject(new Square(15));
        } catch (Exception e) { System.out.println(e); }
        try {
            ObjectInputStream intputStream = new ObjectInputStream(new FileInputStream("file.txt"));
            ((Circle) intputStream.readObject()).draw();
            ((Square) intputStream.readObject()).draw();
        } catch (Exception e) {System.out.println(e); }
    }
}
abstract class Draw implements Serializable { public abstract void draw(); }
class Square extends Draw {
    private final int x;
    public Square(int x) {
        this.x = x;
    }
    public void draw() {
        System.out.println("Square: " + x);
    }
}
class Circle extends Draw implements Serializable {
    private final int x;
    public Circle(int x) {
        this.x = x;
    }
    public void draw() {
        System.out.println("Circle : " + x);
    }
}


Circle : 20
Square: 15
Circle : 15
Square: 20
Compilation Error
NotSerializableException
FileNotFoundException

Doubts

Problems

Topic: Java ObjectInputStream

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App