This set of Java Multiple Choice Questions & Answers (MCQs) on “Serialization & Deserialization”.
1. Which of these is a process of extracting/removing the state of an object from a stream? Answer: d 2. Which of these process occur automatically by java run time system? Answer: d 3. Which of these interface extends DataInput interface? Answer: d 4. Which of these is a method of ObjectInput interface used to deserialize an object from a stream? Answer: c 5. Which of these class extend InputStream class? Answer: b 6. What will be the output of the following Java code? a) 5 7. What will be the output of the following Java code? a) -7 8. What will be the output of the following Java program? a) 1 9. What will be the output of the following Java program? a) 1 10. What will be the output of the following Java program? a) “Success” to the output and “Welcome to ” to the file Answer: a
a) Serialization
b) Externalization
c) File Filtering
d) Deserialization
Clarification: Deserialization is a process by which the data written in the stream can be extracted out from the stream.
a) Serialization
b) Memory allocation
c) Deserialization
d) All of the mentioned
Clarification: Serialization, deserialization and Memory allocation occur automatically by java run time system.
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput
Clarification: ObjectInput interface extends the DataInput interface and supports object serialization.
a) int read()
b) void close()
c) Object readObject()
d) Object WriteObject()
Clarification: None.
a) ObjectStream
b) ObjectInputStream
c) ObjectOutput
d) ObjectInput
Clarification: ObjectInputStream class extends the InputStream class and implements the ObjectInput interface.
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);
}
}
}
b) void
c) serialization
d) deserialization
Clarification: 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
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;
}
}
b) Hello
c) 2.1E10
d) deserialization
Clarification: 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
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);
}
}
}
b) 2
c) 3
d) 0
Clarification: New input stream is linked to steal ‘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
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);
}
}
}
b) 2
c) 3
d) 4
Clarification: 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
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("D:\.txt");
String s="Welcome to .";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("Success");
} catch(Exception e){System.out.println(e);}
}
}
b) only “Welcome to ” to the file
c) compile time error
d) No Output
Clarification: First, it will print “Success” and besides that it will write “Welcome to ” to the file .txt.
