250+ TOP MCQs on Serialization – 1 and Answers

This set of Java Multiple Choice Questions & Answers (MCQs) on “Serialization – 1”.

1. Which of these is a process of writing the state of an object to a byte stream?
a) Serialization
b) Externalization
c) File Filtering
d) All of the mentioned

Answer: a
Clarification: Serialization is the process of writing the state of an object to a byte stream. This is used when you want to save the state of your program to a persistent storage area.

2. Which of these process occur automatically by the java runtime system?
a) Serialization
b) Garbage collection
c) File Filtering
d) All of the mentioned

Answer: a
Clarification: Serialization and deserialization occur automatically by java runtime system, Garbage collection also occur automatically but is done by CPU or the operating system not by the java runtime system.

3. Which of these is an interface for control over serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput

Answer: b
Clarification: None.

4. Which of these interface extends DataOutput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput

Answer: c
Clarification: ObjectOutput interface extends the DataOutput interface and supports object serialization.

5. Which of these is a method of ObjectOutput interface used to finalize the output state so that any buffers are cleared?
a) clear()
b) flush()
c) fflush()
d) close()

Answer: b
Clarification: None.

6. Which of these is method of ObjectOutput interface used to write the object to input or output stream as required?
a) write()
b) Write()
c) StreamWrite()
d) writeObject()

Answer: d
Clarification: writeObject() is used to write an object into invoking stream, it can be input stream or output stream.

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

  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.                 Myclass object2;
  23. 	        FileInputStream fis = new FileInputStream("serial");
  24. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  25.                 object2 = (Myclass)ois.readObject();
  26.                 ois.close();
  27. 	        System.out.println(object2);		    	
  28. 	    }
  29. 	    catch (Exception e) 
  30.             {
  31.                 System.out.print("deserialization" + e);
  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) s=Hello; i=-7; d=2.1E10
b) Hello; -7; 2.1E10
c) s; i; 2.1E10
d) Serialization

Answer: a
Clarification: None.
Output:

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

8. What will be the output of the following Java program?

  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

9. What will be the output of the following Java program?

  1.     import java.io.*;
  2.     class Chararrayinput
  3.     {
  4.         public static void main(String[] args) 
  5.         {
  6. 	    String obj  = "abcdefgh";
  7.             int length = obj.length();
  8.             char c[] = new char[length];
  9.             obj.getChars(0, length, c, 0);
  10.             CharArrayReader input1 = new CharArrayReader(c);
  11.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  12.             int i;
  13.             int j;
  14.             try 
  15.             {
  16. 		while ((i = input1.read()) == (j = input2.read()))
  17.                 {
  18.                     System.out.print((char)i);
  19.                 }
  20.        	    } 
  21.             catch (IOException e) 
  22.             {
  23.                 e.printStackTrace();
  24. 	    }
  25. 	}
  26.     }

a) abc
b) abcd
c) abcde
d) None of the mentioned

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

10. 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. 	        float x;
  22. 	        FileInputStream fis = new FileInputStream("serial");
  23. 	        ObjectInputStream ois = new ObjectInputStream(fis);
  24.                 x = 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) 3
b) 3.5
c) serialization
d) deserialization

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

Leave a Reply

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