Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 185

Given the code fragment from Box.java:

Given the code fragment from Item.java:

Given the information:

The classes Box and Item are encapsulated with getters and setters methods.

The classes Box and Item contains required constructors source code.

and the code fragment:

Which action serializes the b1 object?

    Correct Answer: C

    In Java, for an object to be serialized, all of its fields must also be serializable. Since the Box class contains a list of Item objects, the Item class must implement the Serializable interface for the serialization of Box objects to work correctly. Therefore, implementing the Serializable interface in the Item class is the correct action to serialize the b1 object.

Discussion
ASPushkinOption: C

C) is the right answer but serialization could be done by the readObject and writeObject methods as well.

d7bb0b2Option: C

The correct action to serialize the b1 object is C. Implement the Serializable interface in the Item class. ". In Java, if a class implements Serializable, then all its fields must be serializable as well. If a field is not primitive or does not implement Serializable, then serialization of the class will fail."