Menu
Question Index
...

What will be the output of the following program?

import java.io.*;
public class Draw {
    public static void main(String str[]) {
        try {
            Square sq = new Square(10);
            Circle ci = new Circle(7);
            sq.drawShape();    ci.drawShape();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("shapes.txt"));
            oos.writeObject(sq);    oos.writeObject(ci);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
abstract class DrawObject {
    public abstract void drawShape();
}
class Square extends DrawObject {
    private final int x;
    public Square(int x) {
        this.x = x;
    }
    public void drawShape() {
        System.out.println("The side of a square is : " + x);
    }
}
class Circle extends DrawObject {
    private final int rad;
    public Circle(int rad) {
        this.rad = rad;
    }
    public void drawShape() {
        System.out.println("The radius of a circle is : " + rad);
    }
}


The side of a square is : 10
The radius of a circle is : 7
The side of a square is : 0
The radius of a circle is : 0
The side of a square is : 10
The radius of a circle is : 7
The side of a square is : 10
The radius of a circle is : 7
The side of a square is : 10
The radius of a circle is : 7
The side of a square is : 0
The radius of a circle is : 7
The side of a square is : 10
The radius of a circle is : 7
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Java ObjectOutputStream

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App