My Report

Java Serialization Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. Which of these is a method of ObjectOutput interface used to finalize the output state so that any buffers are cleared?

2. Which of these process occur automatically by the java runtime system?

3. Which of these is an interface for control over serialization and deserialization?

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

 
    import java.io.*;
    class Chararrayinput
    {
        public static void main(String[] args) 
        {
	    String obj  = "abcdefgh";
            int length = obj.length();
            char c[] = new char[length];
            obj.getChars(0, length, c, 0);
            CharArrayReader input1 = new CharArrayReader(c);
            CharArrayReader input2 = new CharArrayReader(c, 1, 4);
            int i;
            int j;
            try 
            {
		while ((i = input1.read()) == (j = input2.read()))
                {
                    System.out.print((char)i);
                }
       	    } 
            catch (IOException e) 
            {
                e.printStackTrace();
	    }
	}
    }

5. Which of these interface extends DataOutput interface?

6. Which of these is method of ObjectOutput interface used to write the object to input or output stream as required?

7. Which of these is a process of writing the state of an object to a byte stream?

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

 
    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  
            {
                Myclass object2;
	        FileInputStream fis = new FileInputStream("serial");
	        ObjectInputStream ois = new ObjectInputStream(fis);
                object2 = (Myclass)ois.readObject();
                ois.close();
	        System.out.println(object2);		    	
	    }
	    catch (Exception e) 
            {
                System.out.print("deserialization" + e);
	        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;
	}
    }

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 
            {
	        float 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);
	    }
        }
    }

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

 
    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;
	}
    }

 

Start practicing “1000 MCQs on Java”, and once you are ready, you can take tests on all topics by attempting our “Java Test Series”.

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.