Serialization And Deserialization(Computer Science > Java Program ) Questions and Answers

Question 1.


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);
}
}
}
  1.    3
  2.    3.5
  3.    serialization
  4.    deserialization
Explanation:-
Answer: Option B. -> 3.5

oos.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



Question 2.


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);
}
}
}
  1.    1
  2.    2
  3.    3
  4.    4
Explanation:-
Answer: Option D. -> 4

oos.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




Question 3.


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();
}
}
}
  1.    abc
  2.    abcd
  3.    abcde
  4.    None of the mentioned
Explanation:-
Answer: Option D. -> None of the mentioned

No 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



Question 4.


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);
}
}
}
  1.    1
  2.    2
  3.    3
  4.    4
Explanation:-
Answer: Option D. -> 4

New 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




Question 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 {
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;
}
}
  1.    -7
  2.    Hello
  3.    2.1E10
  4.    deserialization
Explanation:-
Answer: Option D. -> deserialization

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



Question 6.


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;
}
}
  1.    -7
  2.    Hello
  3.    2.1E10
  4.    deserialization
Explanation:-
Answer: Option D. -> deserialization

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



Question 7.

Which of these class extend InputStream class?


  1.    ObjectStream
  2.    ObjectInputStream
  3.    ObjectOutput
  4.    ObjectInput
Explanation:-
Answer: Option B. -> ObjectInputStream

ObjectInputStream class extends the InputStream class and implements the ObjectInput 

interface.



Question 8.


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);
}
}
}
  1.    5
  2.    void
  3.    serialization
  4.    deserialization
Explanation:-
Answer: Option A. -> 5

oos.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



Question 9.


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;
}
}
  1.    s=Hello; i=-7; d=2.1E10
  2.    Hello; -7; 2.1E10
  3.    s; i; 2.1E10
  4.    Serialization
Explanation:-
Answer: Option A. -> s=Hello; i=-7; d=2.1E10

None.
Output:
$ javac serialization.java
$ java serialization
s=Hello; i=-7; d=2.1E10



Question 10.

Which of these is method of ObjectOutput interface used to write the object to input

or output stream as required?


  1.    write()
  2.    Write()
  3.    StreamWrite()
  4.    writeObject()
Explanation:-
Answer: Option D. -> writeObject()

writeObject() is used to write an object into invoking stream, it can be input stream or output 

stream.