Spring Questions and Answers – Serialization in Java

«
»

This set of tough Java Spring Questions focuses on “Serialization in Java”.

1. Java provides a mechanism, where an object can be represented as a sequence of bytes:-
a) Serialization
b) Compilation
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: a
Explanation: Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.
advertisement

2. After a serialized object has been written into a file, it can be read from the file and deserialized.
a) True
b) False
View Answer

Answer: a
Explanation: The type information and bytes that represent the object and its data can be used to recreate the object in memory.

3. Serialization is JVM independent.
a) True
b) False
View Answer

Answer: a
Explanation: An object can be serialized on one platform and deserialized on an entirely different platform.

4. Classes which are high-level streams that contain the methods for serializing and deserializing an object.
a) ObjectInputStream
b) ObjectOutputStream
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.
advertisement

5. The ObjectOutputStream class contains methods for writing various data types.
a) public final void writeObject(Object x)
b) public final void write(Object x)
c) public final void writeToObject(Object x)
d) all of the mentioned
View Answer

Answer: a
Explanation: The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:
public final void writeObject(Object x) throws IOException

6. The ObjectInputStream class contains methods for writing various data types.
a) public final Object readObject()
b) public final Object read(Object x)
c) public final Object readFromObject(Object x)
d) all of the mentioned
View Answer

Answer: a
Explanation: This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type.

7. For a class to be serialized successfully:
a) The class must implement the java.io.Serializable interface.
b) Every field in the class must be volatile.
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: a
Explanation: The class must implement the java.io.Serializable interface.
All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.
advertisement

8. When serializing an object to a file, the standard convention in Java is to give the file a:
a) .war
b) .java
c) .ser
d) none of the mentioned
View Answer

Answer: c
Explanation: When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.

9. What will be the value of transient field after deserialization:-
a) 0
b) NULL
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: It depends on the data type of field, 0 if int and NULL if String or Object.

10. If the class implements java.io.Serializable, then it is serializable; otherwise, it’s not.
a) True
b) False
View Answer

Answer: a
Explanation: The class must implement the java.io.Serializable interface.
advertisement

Sanfoundry Global Education & Learning Series – Java Spring.
To practice tough questions on all areas of Java Spring, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn