1Z0-829 · Question #18
Given the product class: import java.io.; public class Product implements Serializable { private static float averagePrice = 2.99f; private String description; private transient float price; public…
The correct answer is E. Compilation fails. Compilation fails because the Shop class contains multiple syntax errors that the compiler rejects before execution ever begins. Most critically, the variable p is never declared anywhere in main() - it's used in out.writeObject(p), the cast assignment, and…
Question
Options
- ACookie 2.99 2.99
- BCookie 3.99 2.99
- CCookie 0.0 0.0
- DAn exception is produced at runtime
- ECompilation fails
- FCookie 0.0 2.99
How the community answered
(20 responses)- C5% (1)
- D5% (1)
- E75% (15)
- F15% (3)
Explanation
Compilation fails because the Shop class contains multiple syntax errors that the compiler rejects before execution ever begins. Most critically, the variable p is never declared anywhere in main() - it's used in out.writeObject(p), the cast assignment, and System.out.println(p), but has no type declaration. Additionally, new FileOutputStream("p.ser"))) has an unmatched extra closing parenthesis, and the try-with-resources syntax on the ObjectInputStream line is malformed (a { block opens without a proper try keyword for it).
Options A, B, and F are wrong because they assume deserialization runs and that readObject is invoked - but even if the code compiled, readObject is declared public instead of private, so the serialization framework would silently ignore it (it only hooks into private readObject), making price stay 0.0 as a transient field. Option C (0.0 0.0) is wrong because averagePrice is static and never serialized/reset, so it would remain 2.99. Option D is wrong because the failure happens at compile time, not runtime.
Memory tip: On serialization questions, scan for three things before tracing logic - undeclared variables, mismatched parentheses, and whether readObject/writeObject are private (they must be, or serialization ignores them).
Topics
Community Discussion
No community discussion yet for this question.