Serialization Based Interview Questions

1. Explain the usage of the keyword transient? 
The transient keyword indicates that the value of this variable need not be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (ex: 0 for integers).

2. What are the uses of Serialization? 
Serialization is widely used to:

* To persist data for future use.
* To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
* To "flatten" an object into array of bytes in memory.
* To exchange data between applets and servlets.
* To store user session in Web applications.
* To activate/passivate enterprise java beans.
* To send objects between the servers in a cluster.

3. What is serialization ? 
Serialization is the process of writing the complete state of java object into an output stream. This stream can be file or byte array or stream associated with a TCP/IP socket.

4. What does the Serializable interface do ? 
Serializable is a tagging interface which declares/describes no methods. It is just used to signify the fact that, the current class can be serialized. ObjectOutputStream serializes only those objects which implement this interface.

5. How do I serialize an object to a file ? 
To serialize an object into a stream perform the following steps:

1. Open one of the output streams, for exaample FileOutputStream
2. Chain it with the ObjectOutputStream - Call the method writeObject() providing the instance of a Serializable object as an argument.
3. Close the streams

6. How do I deserilaize an Object? 
To deserialize an object, perform the following steps:

1. Open an input stream
2. Chain it with the ObjectInputStream - Call the method readObject() and cast tthe returned object to the class that is being deserialized.
3. Close the streams

7. What is Externalizable Interface ? 
Externalizable interface is a subclass of Serializable. Java provides Externalizable interface so as to give you more control over what is being serialized and what is not. Using this interface, you can Serialize only the fields of the class you want serialize and ignore the rest.

This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence.

8. What interface must an object implement before it can be written to a stream as an object?
An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

9. What are the rules of serialization 
Some rules of Serialization are:

1. Static fileds are not serialized because they are not part of any one particular object
2. Fileds from the base class are handled only if the parent class itself is serializable
3. Transient fileds are not serialized.