Java Questions & Answers – Serialization & Deserialization

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

Answer: d
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

Answer: d
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

Answer: d
Explanation: ObjectInput interface extends the DataInput interface and supports object serialization.
advertisement
advertisement

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

Answer: c
Explanation: None.

5. Which of these class extend InputStream class?
a) ObjectStream
b) ObjectInputStream
c) ObjectOutput
d) ObjectInput
View Answer

Answer: b
Explanation: ObjectInputStream class extends the InputStream class and implements the ObjectInput interface.

6. What will be the output of the following Java code?

  1.     import java.io.*;
  2.     class streams 
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try 
  7.             {
  8. 	        FileOutputStream fos = new FileOutputStream("serial");
  9. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. 	        oos.writeInt(5);
  11. 	        oos.flush();
  12. 	        oos.close();
  13. 	    }
  14. 	    catch(Exception e)
  15.             {
  16. 	        System.out.println("Serialization" + e);
  17.                 System.exit(0);
  18.             }
  19. 	    try
  20.             {
  21. 	        int z;
  22. 	        FileInputStream fis = new FileInputStream("serial");
  23. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  24. 	        z = ois.readInt();
  25. 	        ois.close();
  26. 	        System.out.println(x);		    	
  27. 	    }
  28. 	    catch (Exception e) 
  29.             {
  30.                 System.out.print("deserialization");
  31. 	        System.exit(0);
  32. 	    }
  33.         }
  34.     }

a) 5
b) void
c) serialization
d) deserialization
View Answer

Answer: a
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:

advertisement
$ javac streams.java
$ java streams
5

7. What will be the output of the following Java code?

advertisement
  1.     import java.io.*;
  2.     class serialization
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try
  7.             {
  8. 	        Myclass object1 = new Myclass("Hello", -7, 2.1e10);
  9. 	        FileOutputStream fos = new FileOutputStream("serial");
  10. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  11. 	        oos.writeObject(object1);
  12. 	        oos.flush();
  13. 	        oos.close();
  14. 	    }
  15. 	    catch(Exception e)
  16.             {
  17. 	        System.out.println("Serialization" + e);
  18.                 System.exit(0);
  19.             }
  20. 	    try
  21.             {
  22. 	        int x;
  23. 	        FileInputStream fis = new FileInputStream("serial");
  24. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  25. 	        x = ois.readInt();
  26. 	        ois.close();
  27. 	        System.out.println(x);		    	
  28. 	    }
  29. 	    catch (Exception e)
  30.             {
  31.                 System.out.print("deserialization");
  32. 	        System.exit(0);
  33. 	    }
  34.         }
  35.     }
  36.     class Myclass implements Serializable
  37.     {
  38. 	String s;
  39. 	int i;
  40. 	double d;
  41. 	Myclass(String s, int i, double d)
  42.         {
  43. 	    this.d = d;
  44. 	    this.i = i;
  45. 	    this.s = s;
  46. 	}
  47.     }

a) -7
b) Hello
c) 2.1E10
d) deserialization
View Answer

Answer: d
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?

  1.     import java.io.*;
  2.     class streams
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try
  7.             {
  8. 	        FileOutputStream fos = new FileOutputStream("serial");
  9. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. 	        oos.writeFloat(3.5);
  11. 	        oos.flush();
  12. 	        oos.close();
  13. 	    }
  14. 	    catch(Exception e)
  15.             {
  16. 	        System.out.println("Serialization" + e);
  17.                 System.exit(0);
  18.             }
  19. 	    try
  20.             {
  21. 	        FileInputStream fis = new FileInputStream("serial");
  22. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  23. 	        ois.close();
  24. 	        System.out.println(ois.available());		    	
  25. 	    }
  26. 	    catch (Exception e)
  27.             {
  28.                 System.out.print("deserialization");
  29. 	        System.exit(0);
  30. 	    }
  31.         }
  32.     }

a) 1
b) 2
c) 3
d) 0
View Answer

Answer: d
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?

  1.     import java.io.*;
  2.     class streams
  3.     {
  4.         public static void main(String[] args)
  5.         {
  6.             try
  7.             {
  8. 	        FileOutputStream fos = new FileOutputStream("serial");
  9. 	        ObjectOutputStream oos = new ObjectOutputStream(fos);
  10. 	        oos.writeFloat(3.5);
  11. 	        oos.flush();
  12. 	        oos.close();
  13. 	    }
  14. 	    catch(Exception e)
  15.             {
  16. 	        System.out.println("Serialization" + e);
  17.                 System.exit(0);
  18.             }
  19. 	    try
  20.             {
  21. 	        FileInputStream fis = new FileInputStream("serial");
  22. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  23. 	        System.out.println(ois.available());		    	
  24. 	    }
  25. 	    catch (Exception e) 
  26.             {
  27.                 System.out.print("deserialization");
  28. 	        System.exit(0);
  29. 	    }
  30.         }
  31.     }

a) 1
b) 2
c) 3
d) 4
View Answer

Answer: d
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?

  1. import java.io.FileOutputStream;  
  2. public class FileOutputStreamExample
  3. {  
  4.     public static void main(String args[])
  5.     {    
  6.            try
  7.            {    
  8.              FileOutputStream fout=new FileOutputStream("D:\\sanfoundry.txt");    
  9.              String s="Welcome to Sanfoundry.";    
  10.              byte b[]=s.getBytes();//converting string into byte array    
  11.              fout.write(b);    
  12.              fout.close();    
  13.              System.out.println("Success");    
  14.             }  catch(Exception e){System.out.println(e);}    
  15.       }    
  16. }

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

Answer: a
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.