Serialization & Deserialization

1

What is the output of this 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);
	    }
        }
    }
A. 3
B. 3.5
C. serialization
D. deserialization

2

What is the output of this 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);
	    }
        }
    }
A. 1
B. 2
C. 3
D. 4

3

What is the output of this 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();
	    }
	}
    }
A. abc
B. abcd
C. abcde
D. None of the mentioned

4

What is the output of this 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);
	    }
        }
    }    
A. 1
B. 2
C. 3
D. 4

5

What is the output of this 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;
	}
    }
A. -7
B. Hello
C. 2.1E10
D. deserialization