Interview questions on serialization in java for experienced

 Part 1

Interview questions on serialization in java for experienced covered in this post:


    Interview questions on serialization in java for experienced


            Can you tell me what are the Serializable and Externalizable?

    -          Serializable and Externalizable are the Interfaces.

    -          Serializable interface Externalizable interface belongs to java.io. package.

     

            What are the advantages and disadvantages of Serialization?

    -          Advantages of Serialization.

    1.       Convert the object onto the byte stream and transfer it through the network.

    2.       A converted byte stream can save into a file or database.

    3.       Third-party services do not require implementing serialization.

    4.       Serialization allows java to perform Encryption, decryption, Authentication etc.

    -          Disadvantages of Serialization.

    1.       Serialization is default serialization so unnecessary we have to implement full serialization.

    2.       Serialization is inefficient when it comes to memory utilization.

    3.       Sometimes the byte stream does not convert into objects completely which leads to error.


     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

     

            What is Serialization? Explain the need of serialization?

    -          Serialization is a process of converting the state of object into byte stream.

    -          Serialization takes care by JVM.

    -          In Serialization it’s mandatory to save total object.

    -          Need for the serialization is as follow:

    1.       Transfer the state of object through the network.

    2.       Can save the state of object into file.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.io.Serializable; 

    public class Student implements Serializable { 

          private int id;

          private static String name;

          private String dept;     

          public Student(int id, String name, String dept) {

                super();

                this.id = id;

                this.name = name;

                this.dept = dept;

          }

          public int getId() {

                return id;

          }

          public void setId(int id) {

                this.id = id;

          } 

          public String getName() {

                return name;

          }

          public void setName(String name) {

                this.name = name;

          }

          public String getdept() {

                return dept;

          }

          public void setdept(String dept) {

                this.dept = dept;

          }

    } 

    package simplifiedjava.crackedInterview; 

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream; 

    public class SerializationDemo { 

          public static void main(String[] args) throws IOException, ClassNotFoundException {           

                Student s1 = new Student(100,"Yogesh","IT");           

                File file1 = new File("stud1.ser");

                FileOutputStream fos = new FileOutputStream(file1);

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(s1);           

                File file2 = new File("stud1.ser");

                FileInputStream fis = new FileInputStream(file2);

                ObjectInputStream ois = new ObjectInputStream(fis);

                Student s2 = (Student) ois.readObject();           

                System.out.println("Student ID :" + s2.getId());

                System.out.println("Student Name :"+ s2.getName());

                System.out.println("Student Dept :"+ s2.getdept());

          }

    }

    Output:

    Student ID :100

    Student Name :Yogesh

    Student Dept :IT

     

            What is deserialization?

    -          Deserialization is the reverse process of serialization.

    -          The reverse process of creating the object from a stored sequence of bytes.

    -          Please refer to the above example for deserialization. 

     

            What are the methods used for Serialization and Deserialization?

    -          For Serialization we have to use writeObject() method which belongs to ObjectOutputStream.

    -          For Deserialization we have to use readObject() method which belongs to ObjectInputStream.

     

            What is SerialVersionUID? Who provides this SerialVersionUID? How will you create it?

    -          SerialVersionUID is a unique identifier of the class.

    -          SerialVersionUID represent the version of the class. This comes into the picture while serialization and deserialization.

    -          JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization.

    -          the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.

    -          You can refer the below image to generate the SerialVerionUID.

    Interview questions on serialization in java for experienced

            What is the Externalization? What is the role of Externalizable interface?

    -          Externalization is customizing serialization.

    -          In Serialization, it’s always possible to save the total object to the file and it’s not possible to save part of the object which may create a performance issue. To cover come to this issue we should go for externalization.

    -          In externalization, everything takes care of by the programmer and JVM doesn’t have any control over it.

    -          Please refer the blelow example.

    package simplifiedjava.crackedInterview; 

    import java.io.Externalizable;

    import java.io.IOException;

    import java.io.ObjectInput;

    import java.io.ObjectOutput; 

    public class Account implements Externalizable{ 

          long accountId;

          String userName;

          int pin;

          int token;     

          public Account() {

                super();

          }

          public Account(long accountId, String userName, int pin, int token) {

                super();

                this.accountId = accountId;

                this.userName = userName;

                this.pin = pin;

                this.token = token;

          } 

          @Override

          public void writeExternal(ObjectOutput oos) throws IOException {

                oos.writeLong(accountId);

                oos.writeObject(userName);

                oos.writeInt(pin);     

          }         

          @Override

          public void readExternal(ObjectInput ois) throws IOException, ClassNotFoundException {

                long accountIdV1 = (long)ois.readLong();

                String userNameV1 = (String)ois.readObject();

                int pinV1 = (int)ois.readInt();

          }

    }

    package simplifiedjava.crackedInterview;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream; 

    public class ExternalizationDemo { 

          public static void main(String[] args) throws IOException, ClassNotFoundException {

                Account account = new Account(1001L,"Yogesh Sanas",5555,9800);        

                FileOutputStream fos = new FileOutputStream("acc.ser");

                ObjectOutputStream oos = new ObjectOutputStream(fos);

                oos.writeObject(account);           

                FileInputStream fis = new FileInputStream("acc.ser");

                ObjectInputStream ois = new ObjectInputStream(fis);

                Account accountV1 = (Account)ois.readObject();           

                System.out.println(accountV1.accountId);

                System.out.println(accountV1.userName);

                System.out.println(accountV1.pin);

                System.out.println(accountV1.token); 

                System.out.println(account);

          }

    }

     

            What are the methods used for Externalization?

    -          There are couple of methods which are used in Externalization Interface.

    1.       writeExternal():This method can be used to write the custom object for serialization.

    2.        readExternal():This method can be used to read the custom object either from the network or file.

     

            What is marker interface? Can you tell me the examples of marker interface?

    -          Empty interfaces are called marker interfaces.

    -          Marker interfaces don’t have any method, variable or constant.

    -          Marker interfaces are used to inform the JVM that a class implementing this interface will have some special behaviour.

    -          There are some marker interfaces in java which are as follows.

    1.       Serializable Interface.

    2.       Clonable Interface.

    3.       Remote Interface.

    4.       ActionListener Interface.

     

            What is the difference between Serialization and Externalization?

     

    Serialization

    Externalization

    1.

    Serialization is default Serialization.

    Externalization is customizing Serialization.

    2.

    Everything takes care of by JVM in serialization.

    Everything takes care of by the Programmer in Externalization.

    3.

    Serialization is not capable to save the required part of the object.

    It’s also possible to save part of the object or full object in Externalization.

    4.

    Performance is low as compared to Externalization.

    Performance is high as compared to serialization.

    5.

    If you want to serialize a full object then serialization is a good option.

    If you want to serialize a partial object then externalization is a good option.

    6.

    For serialization, we have to implement a Serializable interface.

    For externalization, we have to implement an Externalizable interface.

    7.

    The transient keyword is required if you don’t want to make a particular field serialize.

    The transient keyword is not required because this is customizing serialization.

    8.

    A No-arg constructor is not required.

    A No-arg constructor is required at the time of deserialization otherwise we will get InvalidClassException.




    • Java interview questions and answers all MNC - Click here
    • Basic core java interview questions and answers for freshers - Click here
    • Core java interview questions for 3 years experience - Click here
    • Core java interview questions and answers for 3-5 years exp - Click here
    • Core java interview questions and Answers for 5 - 7 Years exp - Click here
    • Basic Java Interview Questions and Answers - Click here
    • Java interview questions and answers on oops - Click here
    • Java interview questions and answers on Strings - Click here
    • Java interview questions on exception handling - Click here
    • Interview questions on multithreading in java for experienced - Click here
    • Interview questions on serialization in java for experienced - Click here

    • Interview questions on inner class in java for experienced - Click here
    • Interview questions on Collections in java for experienced - Click here

    Upcoming questions in next post:


    Previous Post                                                                    Next Post



    Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. 

    I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. 

    You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. 

    Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions. 

    My total experience is 10. Initially I had worked on some support projects and then I moved to java projects. I had worked with many multi-national companies like R-Systems, Tata Consultancy Services, Cybage Softwares.  Fortunately, TCS and Cybage has given me an opportunity to take interviews for experienced candidates. I have conducted more than 1000 interviews by now.

    Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. 

    I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. 

    I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! 

    Please reach out to me for personal training for interview preparation. 

    You can reach out to me at mncjavainterview@gmail.com.

     



    Post a Comment

    0 Comments