Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| |||||||||||
Core Java - Interview Questions and Answers for 'Serialization' - 21 question(s) found - Order By Newest | |||||||||||
Frequently asked in Cognizant (CTS) | |||||||||||
| |||||||||||
Ans. Storing the state of an object in a file or other medium is called serialization. Classes can communicate only if they are built together ( as they need Byte code for communication ). What if we need to enable communication between different applications ( i.e they have been built independently or even they reside at different locations ), We need a mechanism that will transfer the Bean state to a Medium than can be transferred to the receiving application. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   serialization Asked in 18 Companies basic   frequent | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. It's weird that compiler doesn't complain if we declare transient with static variable because it makes no sense. At least a warning message saying "transient is useless in this situation" would have helped with code cleaning. Static variables are never serialized and transient is an indication that the specified variable shouldn't be serialized so its kind of double enforcement not to serialize. It could be that as it makes no different to the variable behavior and hence using both keywords with a variable are permitted. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  static  transient  serialization Asked in 1 Companies expert   rare | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. 1. Objects are serialized and not classes and hence Static variables are ignored. 2. Transient is an explicit declaration to ignore the variable during serialization and hence transient instance variables are ignored too. 3. Base class instance variables if the base class hasn't been declared serializable. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization Asked in 1 Companies intermediate   frequent | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. No. Only Object and its members are serialized. Static variables are shared variables and doesn't correspond to a specific object. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization   java   oops   static   static variables Asked in 1 Companies intermediate   rare | |||||||||||
Try 2 Question(s) Test | |||||||||||
| |||||||||||
Ans. One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializable Exception. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization   java   oops   object expert | |||||||||||
| |||||||||||
Ans. It in Java is used to indicate that a field should not be serialized. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   serialization   transient   java keywords Asked in 39 Companies intermediate   frequent | |||||||||||
Try 2 Question(s) Test | |||||||||||
| |||||||||||
Ans. The problem with serialized singleton class is that whenever we deserialize it, it will create a new instance of the class. To overcome this scenario all we need to do is to provide the implementation of readResolve() method. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  singleton  serialization Asked in 1 Companies | |||||||||||
| |||||||||||
Ans. Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   serialization   deserialization   serialization vs deserialization Asked in 2 Companies basic   frequent | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. Externalizable interface is used to write the state of an object into a byte stream in compressed format. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   serialization   externalizable interface   interface Asked in 3 Companies | |||||||||||
| |||||||||||
Ans. Serializable is a marker interface whereas externalizable is not. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   serialization   externalizable   interface   marker interface | |||||||||||
| |||||||||||
Ans. Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   serialization   externalizable interface Asked in 2 Companies intermediate   rare | |||||||||||
| |||||||||||
Ans. For Cloning-exception,For deserialization-read.resolve() | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  singleton  cloneable  serializable  serialization  cloning Asked in 1 Companies | |||||||||||
| |||||||||||
Ans. Transient variables are variable that cannot be serialized. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   serialization   transient Asked in 15 Companies intermediate   rare | |||||||||||
| |||||||||||
Ans. Though we are trying to serialize BuggyBread1 object but we haven't declared the class to implement Serializable. This will throw java.io.NotSerializableException upon execution. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling | |||||||||||
| |||||||||||
Ans. No, It will throw java.io.NotSerializableException. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   io   file   fileio   coding   code   serialization   notserializableexception   exception   file handling | |||||||||||
| |||||||||||
Ans. Yes. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   io   file   fileio   coding   code   serialization   yesno  file handling | |||||||||||
| |||||||||||
Ans. We can serialize final variables | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization   java   file io | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. Everytime an object is serialized the java serialization mechanism automatically computes a hash value by passing the meta information for the class. This id is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialVersionUID  serialization Asked in 3 Companies expert   rare | |||||||||||
| |||||||||||
Ans. // Java code for serialization and deserialization // of a Java object import java.io.*; class Demo implements java.io.Serializable { public int a; public String b; // Default constructor public Demo(int a, String b) { this.a = a; this.b = b; } } class Test { public static void main(String[] args) { Demo object = new Demo(1, "geeksforgeeks"); String filename = "file.ser"; // Serialization try { //Saving of object in a file FileOutputStream file = new FileOutputStream(filename); ObjectOutputStream out = new ObjectOutputStream(file); // Method for serialization of object out.writeObject(object); out.close(); file.close(); System.out.println("Object has been serialized"); } catch(IOException ex) { System.out.println("IOException is caught"); } Demo object1 = null; // Deserialization try { // Reading the object from a file FileInputStream file = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(file); // Method for deserialization of object object1 = (Demo)in.readObject(); in.close(); file.close(); System.out.println("Object has been deserialized "); System.out.println("a = " object1.a); System.out.println("b = " object1.b); } catch(IOException ex) { System.out.println("IOException is caught"); } catch(ClassNotFoundException ex) { System.out.println("ClassNotFoundException is caught"); } } } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization Asked in 1 Companies | |||||||||||
| |||||||||||
Ans. It depends on how we are serializing. The Serialization API doesn't worry about private variables and convert it into binary representation. If we are using a library to map it to JSON / XML using XML Mappers, it may create trouble. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization | |||||||||||
| |||||||||||
Ans. we use serialization when we send response to client in JSON format.(the process of converting model object into json is nothing but serialization | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  serialization Asked in 1 Companies | |||||||||||
| |||||||||||