nerdexam
Oracle

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…

Using Java I/O API

Question

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 String toString() { return title; } } public class Game extends Software { private int players; public Game(String title, int players) { super(title); this.players = players; } System.out.print("Game "); public String toString() { return super.toString() + " " + players; } } import java.io.*; public class AppStore { public static void main(String[] args) { Software s = new Game("Chess", 2); try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("game.ser"))); out.writeObject(s); } catch (Exception e) { System.out.println("write error"); } try { ObjectInputStream in = new ObjectInputStream(new FileInputStream("game.ser"))); s = (Software)in.readObject(); } catch (Exception e) { System.out.println("read error"); } System.out.println(s); } } What is the result?

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)
  • A
    2% (1)
  • B
    42% (18)
  • C
    16% (7)
  • D
    2% (1)
  • E
    30% (13)
  • F
    7% (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:

  1. new Game("Chess", 2) triggers instance initializers (the bare System.out.print statements in each class body), printing "Software Game "
  2. out.writeObject(s) serializes the object - no additional output
  3. in.readObject() deserializes the object - Java does NOT invoke constructors or instance initializers for Serializable classes during deserialization; only the first non-Serializable ancestor's (Object) no-arg constructor is called
  4. System.out.println(s) calls Game.toString()super.toString() + " " + players"Chess 2"

Actual output: Software Game Chess 2 → Option E

Why the distractors are wrong:

  • A / D: Show 0 for players, implying the field wasn't restored - but int players is not transient, so it deserializes correctly as 2
  • 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

#Instance initializer blocks#Serialization/Deserialization#Inheritance#Method overriding

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice