1Z0-829 · Question #19
Given: import java.io.Serializable; public class Software implements Serializable { private String title; public Software(String title) { this.title = title; } System.out.print("Software "); public…
The correct answer is B. Software Game Software Game Chese 2. There is a significant issue with the stated answer - based on the Java specification, option E is actually correct, not B. Here is the full breakdown: What the code does: 1. new Game("Chess", 2) triggers instance initializers (the bare System.out.print statements in each class…
Question
Options
- ASoftware Game Chess 0
- BSoftware Game Software Game Chese 2
- CSoftware game write error
- DSoftware Game Software Game chess 0
- ESoftware Game Chess 2
- FSoftware Game read error
How the community answered
(43 responses)- A2% (1)
- B42% (18)
- C16% (7)
- D2% (1)
- E30% (13)
- F7% (3)
Explanation
There is a significant issue with the stated answer - based on the Java specification, option E is actually correct, not B. Here is the full breakdown:
What the code does:
new Game("Chess", 2)triggers instance initializers (the bareSystem.out.printstatements in each class body), printing "Software Game "out.writeObject(s)serializes the object - no additional outputin.readObject()deserializes the object - Java does NOT invoke constructors or instance initializers forSerializableclasses during deserialization; only the first non-Serializable ancestor's (Object) no-arg constructor is calledSystem.out.println(s)callsGame.toString()→super.toString() + " " + players→ "Chess 2"
Actual output: Software Game Chess 2 → Option E
Why the distractors are wrong:
- A / D: Show
0for players, implying the field wasn't restored - butint playersis nottransient, so it deserializes correctly as2 - C / F: No write or read error occurs (assuming the extra
)typos in the code are unintentional) - B (stated answer): Would require instance initializers to fire again during deserialization - this contradicts the Java Object Serialization Specification
Memory tip: For Serializable objects, deserialization bypasses constructors entirely (using Unsafe.allocateInstance under the hood). Only Externalizable objects have their no-arg constructor called on read. "Serializable = silent restore; Externalizable = constructor required."
Bottom line: This question appears to contain an error. The marked answer B reflects a common exam misconception that deserialization re-runs constructors/initializers - it does not.
Topics
Community Discussion
No community discussion yet for this question.