This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Serialization & Deserialization”.
1. Which of these is a process of extracting/removing the state of an object from a stream?
a) Serialization
b) Externalization
c) File Filtering
d) Deserialization
View Answer
Explanation: Deserialization is a process by which the data written in the stream can be extracted out from the stream.
2. Which of these process occur automatically by java run time system?
a) Serialization
b) Memory allocation
c) Deserialization
d) All of the mentioned
View Answer
Explanation: Serialization, deserialization and Memory allocation occur automatically by java run time system.
3. Which of these interface extends DataInput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput
View Answer
Explanation: ObjectInput interface extends the DataInput interface and supports object serialization.
4. Which of these is a method of ObjectInput interface used to deserialize an object from a stream?
a) int read()
b) void close()
c) Object readObject()
d) Object WriteObject()
View Answer
Explanation: None.
5. Which of these class extend InputStream class?
a) ObjectStream
b) ObjectInputStream
c) ObjectOutput
d) ObjectInput
View Answer
Explanation: ObjectInputStream class extends the InputStream class and implements the ObjectInput interface.
6. What will be the output of the following Java code?
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);
}
}
}
a) 5
b) void
c) serialization
d) deserialization
View Answer
Explanation: 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
7. What will be the output of the following Java code?
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;
}
}
a) -7
b) Hello
c) 2.1E10
d) deserialization
View Answer
Explanation: 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
8. What will be the output of the following Java 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);
}
}
}
a) 1
b) 2
c) 3
d) 0
View Answer
Explanation: 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
9. What will be the output of the following Java 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);
}
}
}
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: 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
10. What will be the output of the following Java program?
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("D:\\sanfoundry.txt");
String s="Welcome to Sanfoundry.";
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);}
}
}
a) “Success” to the output and “Welcome to Sanfoundry” to the file
b) only “Welcome to Sanfoundry” to the file
c) compile time error
d) No Output
View Answer
Explanation: First, it will print “Success” and besides that it will write “Welcome to Sanfoundry” to the file sanfoundry.txt.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Practice Information Technology MCQs
- Check Java Books
- Apply for Java Internship
- Check Programming Books