Java Serialization readObject Access Modifier
Short answer
B Cookie 0.0 2.99
Question
- 
Key Takeaways
Tests knowledge that the deserialization hook must be private; if public, it is ignored and transient variables retain their initial defaults.
Determines the output of a Java program involving serialization when the private readObject method is incorrectly declared as public, causing transient fields to remain default values.
Common mistake: Learners often select C or D, assuming the custom logic runs regardless of visibility, missing the strict contract requirements of the Serialization API.
Expert Analysis
Why the Answer Is Correct
The correct output is B because the readObject method in the Product class is declared as public. According to Java's serialization specification, for the custom readObject method to be invoked during deserialization, it must have the signature private void readObject(ObjectInputStream ois). Since it is public, the reflection mechanism does not recognize it as a valid callback. Consequently, the default deserialization process occurs, leaving the transient field price at its default primitive value of 0.0, while the non-transient name is restored.
Why the Other Options Are Wrong
Options C and D imply that the readObject method executed successfully, which is incorrect due to the access modifier violation. Option A is incorrect because the code compiles fine; the error is logical/runtime behavior, not syntax. Option E is incorrect because no exception is thrown; the serialization simply proceeds without invoking the custom logic.
Community Comment Notes
Community consensus correctly identifies B. As user SrinivasJasti noted, "Since it is public,loses its special status in the serialization process." User Langton also confirmed that "for readObject method to be called it has to be difined as private which is not the case here."
Exam Strategy
Memorize the exact signature for serialization hooks: they must be private. Any deviation in visibility (protected, package-private, public) causes the JVM to ignore them.
Frequently Asked Questions
Can readObject be protected?
What happens to transient fields if readObject is ignored?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 4 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 4
-
supersquax 👍 3B correct, verified in online compiler!
-
Langton 👍 2Answer is B because for readObject() method to be called it has to be difined as private which is not the case here
-
SrinivasJasti 👍 1 Answered BThe Java Serialization API is designed to recognize only a private readObject method for deserialization purposes. Since it is public ,loses its special status in the serialization process.The transient fields are not restored to their intended values threfore price remains 0.0 leading to Cookie 0.0 2.99
-
xplorerpj 👍 1 Answered BB is correct
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.