My Report

Java Serialization Test – 3


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 interface extends DataInput interface?

2. Which of these class extend InputStream class?

3. 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);
	    }
        }
    }

4. Which of these is a process of extracting/removing the state of an object from a stream?

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

6. 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);
	    }
        }
    }

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

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

9. Which of these process occur automatically by java run time system?

10. Which of these is a method of ObjectInput interface used to deserialize an object from a stream?


 

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.