Menu
Topics Index
...
`

Files1916


If you need explanation Read this topic

If you need Answer Take test on this topic

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

A. 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
B. 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
C. 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
D. The side of a square is : 10
The radius of a circle is : 7
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java ObjectOutputStream

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 E. NotSerializableException arises. After printing the statements in println methods of drawShape() methods for bothe the instances, the exception arises since both the classes Circle and Square are not serialized and writeObject() throws the exception

Posted by Nikhil Sulibhavi    2014-05-12 12:48:20


An object is serializable only if its class or its super class implements the Serializable interface. Here none of DrawObject or Square and Circle classes implement Serializable interface. So, NotSerializableException will be thrown which is caught by the catch statement which prints corresponding Exception details. There is no problem with drawShape() calls on Square and Circle objects, though. To fix this problem, either make DrawObject implement Serializable or make both Square and Circle classes implement Serializable. So, the answer is E.

Posted by Shaileshwar Sharma    2014-05-12 13:11:59


No winner for this dose. Correct option is 'F' :(.

Posted by Merit Campus    2014-05-13 04:25:15


Merit Campus: I think this program won't terminate abruptly. writeObject() method (at LINE 9) throws a NotSerializableException which is caught by the catch block and it prints details of the exception (e.printStackTrace()). This exception information is part of the output. The program would continue executing if there were statements after the catch block. So along with the output produced by drawShape() calls (on Square and Circle objects) exception info. would also be displayed. So I guess the answer must be E if we go by the options provided here. Correct me if I am wrong.

Posted by Shaileshwar Sharma    2014-05-13 09:41:25


Shaileshwar Sharma, what you said is true. Although it is throwing an exception, since we are catching the exception and printing the stack trace the program is ending normally. So the correct answer is E.

Posted by Merit Campus    2014-05-14 07:10:11


So Nikhil Sulibhavi is the winner for this dose and he will get the recharge link. Shaileshwar Sharma will also get a recharge link for highlighting the issue.

Posted by Merit Campus    2014-05-14 07:11:15


Thank you Merit Campus and Shaileshwar Sharma

Posted by Nikhil Sulibhavi    2014-05-14 07:19:32


© meritcampus 2019

All Rights Reserved.

Open In App