nerdexam
Oracle

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…

Using Java I/O API

Question

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 Product(String description, float price) { this.description = description; this.price = price; } public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); price = averagePrice; } public String toString() { return description+" "+price+" "+averagePrice; } } And the shop class: import java.io.*; public class Shop { public static void main(String[] args) { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("p.ser"))); out.writeObject(p); ObjectInputStream in = new ObjectInputStream(new FileInputStream("p.ser"))) { p = (Product)in.readObject(); } catch (Exception e) { e.printStackTrace(); } System.out.println(p); } } What is the result?

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)
  • C
    5% (1)
  • D
    5% (1)
  • E
    75% (15)
  • F
    15% (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

#serialization#readObject contract#try-with-resources syntax#compilation errors

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice