Sail E0 Webinar
Question


What is the output of this program?


import java.io.*;
class serialization {
public static void main(String[] args) {
try {
Myclass object1 = new Myclass("Hello", -7, 2.1e10);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
int x;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
x = ois.readInt();
ois.close();
System.out.println(x);
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
class Myclass implements Serializable {
String s;
int i;
double d;
Myclass(String s, int i, double d){
this.d = d;
this.i = i;
this.s = s;
}
}
Options:
A .  -7
B .  Hello
C .  2.1E10
D .  deserialization
Answer: Option D

x = ois.readInt(); will try to read an integer value from the stream 'serial' created before, 

since stream contains an object of Myclass hence error will occur and it will be catched 

by catch printing deserialization.
Output:
$ javac serialization.java
$ java serialization
deserialization



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers