250+ TOP MCQs on Serialization & Deserialization and Answers

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?
a) Serialization
b) Externalization
c) File Filtering
d) Deserialization

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

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

Answer: d
Clarification: 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()

Answer: c
Clarification: None.

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

Answer: b
Clarification: 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

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

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

  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

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

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

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

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

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

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:\.txt");    
  9.              String s="Welcome to .";    
  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 ” to the file
b) only “Welcome to ” to the file
c) compile time error
d) No Output

  250+ TOP MCQs on MouseEvent, TextEvent & WindowEvent Class and Answers

Answer: a
Clarification: First, it will print “Success” and besides that it will write “Welcome to ” to the file .txt.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top