Serialization


  1. How an object can become serializable?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    A Java object is serializable if class or any its superclass implements java.io.Serializable or its subinterface java.io.Externalizable.


  1. What is the output of this program?
    import java.io.*;
    public class serialization_Example
    {
    public static void main(String[] args)
    {
    try
    {
    Newclass obj1 = new Newclass("InterviewMania", -10, 3.2e20);
    FileOutputStream fos = new FileOutputStream("serial");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(obj1);
    oos.flush();
    oos.close();
    }
    catch(Exception e)
    {
    System.out.println("Serialization" + e);
    System.exit(0);
    }
    try
    {
    Newclass obj2;
    FileInputStream fis = new FileInputStream("serial");
    ObjectInputStream ois = new ObjectInputStream(fis);
    obj2 = (Newclass)ois.readObject();
    ois.close();
    System.out.println(obj2);
    }
    catch (Exception e)
    {
    System.out.print("deserialization" + e);
    System.exit(0);
    }
    }
    }
    class Newclass implements Serializable
    {
    String str;
    int k;
    double p;
    Newclass (String str, int k, double p)
    {
    this.p = p;
    this.k = k;
    this.str = str;
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Myclass@776ec8df



  1. What is the output of this program?
    import java.io.*;
    public class serialization_Example
    {
    public static void main(String[] args)
    {
    try
    {
    Newclass obj1 = new Newclass("InterviewMania", -10, 3.2e20);
    FileOutputStream fos = new FileOutputStream("serial");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(obj1);
    oos.flush();
    oos.close();
    }
    catch(Exception e)
    {
    System.out.println("Serialization" + e);
    System.exit(0);
    }
    try
    {
    int p;
    FileInputStream fis = new FileInputStream("serial");
    ObjectInputStream ois = new ObjectInputStream(fis);
    p = ois.readInt();
    ois.close();
    System.out.println(p);
    }
    catch (Exception e)
    {
    System.out.print("deserialization");
    System.exit(0);
    }
    }
    }
    class Newclass implements Serializable
    {
    String str;
    int k;
    double q;
    Newclass(String str, int k, double q)
    {
    this.q = q;
    this.k = k;
    this.str = str;
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    p = 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.


  1. What is the output of this program?
    import java.io.*;
    public class streams_Example
    {
    public static void main(String[] args)
    {
    try
    {
    FileOutputStream FOS = new FileOutputStream("serial");
    ObjectOutputStream OOS = new ObjectOutputStream(FOS);
    OOS.writeDouble(1.5);
    OOS.flush();
    OOS.close();
    }
    catch(Exception e)
    {
    System.out.println("Serialization" + e);
    System.exit(0);
    }
    try
    {
    float p;
    FileInputStream FIS = new FileInputStream("serial");
    ObjectInputStream OIS = new ObjectInputStream(FIS);
    p = OIS.readInt();
    OIS.close();
    System.out.println(p);
    }
    catch (Exception e)
    {
    System.out.print("deserialization");
    System.exit(0);
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    OOS.writeFloat(1.5); writes in output stream which is extracted by p = OIS.readInt(); and stored in p hence p contains 1.5.



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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

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