Serialization And Deserialization(Computer Science > Java Program ) Questions and Answers
What is the output of this program?
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
float 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);
}
}
}
Explanation:-
Answer: Option B. -> 3.5oos.writeFloat(3.5); writes in output stream which is extracted by x = ois.readInt(); and stored
in x hence x contains 3.5.
Output:
$ javac streams.java
$ java streams
3.5
What is the output of this program?
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(ois.available());
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
Explanation:-
Answer: Option D. -> 4oos.writeFloat(3.5); writes 3.5 in output stream. A new input stream is linked to stream 'serials',
an object 'ois' of ObjectInputStream is used to access this newly created stream, ois.available()
gives the total number of byte in the input stream since a float was written in the stream thus the
stream contains 4 byte, hence 4 is returned and printed.
Output:
$ javac streams.java
$ java streams
4
What is the output of this program?
import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdefgh";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
int i;
int j;
try {
while ((i = input1.read()) == (j = input2.read())) {
System.out.print((char)i);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:-
Answer: Option D. -> None of the mentionedNo output is printed. CharArrayReader object input1 contains string “abcdefgh†whereas
object input2 contains string "bcde", when while((i=input1.read())==(j=input2.read())) is
executed the starting character of each object is compared since they are unequal control
comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
What is the output of this program?
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
ois.close();
System.out.println(ois.available());
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
Explanation:-
Answer: Option D. -> 4New input stream is linked to streal 'serials', an object 'ois' of ObjectInputStream is used to
access this newly created stream, ois.close(); closes the stream hence we can't access the
stream and ois.available() returns 0.
Output:
$ javac streams.java
$ java streams
0
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;
}
}
Explanation:-
Answer: Option D. -> deserializationx = 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
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;
}
}
Explanation:-
Answer: Option D. -> deserializationx = 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
What is the output of this program?
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
int z;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
z = ois.readInt();
ois.close();
System.out.println(x);
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
Explanation:-
Answer: Option A. -> 5oos.writeInt(5); writes integer 5 in the Output stream which is extracted by z = ois.readInt();
and stored in z hence z contains 5.
Output:
$ javac streams.java
$ java streams
5
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 {
Myclass object2;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
object2 = (Myclass)ois.readObject();
ois.close();
System.out.println(object2);
}
catch (Exception e) {
System.out.print("deserialization" + e);
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;
}
}
Explanation:-
Answer: Option A. -> s=Hello; i=-7; d=2.1E10None.
Output:
$ javac serialization.java
$ java serialization
s=Hello; i=-7; d=2.1E10