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.

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.
advertisement
advertisement

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.

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
Note: Join free Sanfoundry classes at Telegram or Youtube

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.
advertisement

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.

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.