Java 21 Certification Guide - Part 4
Welcome to the fourth article in my series on preparing for the Java 21 certification.
Over the next several posts, we’ll walk through the most important language changes, APIs, and tricky details you’ll need to know for the exam.
In this article, we’ll focus on Serializable and IO operations. These operations have existed since Java 1.4; however, we don't use them every day, so it can be a little hard.
To make things practical, we’ll analyze a question in the style of the certification exam.
❓ Question
What is the output for this code?
public class Question {
public static void main(String[] args) {
Child child = new Child(20, "abc", "pass");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("child.ser"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("child.ser"))) {
child.age = 15;
child.id = 2;
oos.writeObject(child);
Child deserializedChild = (Child) ois.readObject();
System.out.println(deserializedChild);
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error during the serialization");
}
}
static class Parent {
int id;
public Parent() {
this.id = 10;
System.out.println("Parent constructor called, id = " + id);
}
}
static class Child extends Parent implements Serializable {
int age = 20;
String name;
transient String password;
transient boolean persisted;
public Child() {
this.persisted = true;
}
public Child(int age, String name, String password) {
this.age = age;
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "age=" + age +
", name='" + name + '\'' +
", password='" + password + '\'' +
", persisted='" + persisted + '\'';
}
}
}A)
Error during the serialization
B)
Parent constructor called, id = 10
Parent constructor called, id = 2
age=15, name='abc', password='pass', persisted='true'
C)
Parent constructor called, id = 10
Parent constructor called, id = 10
age=15, name='abc', password='null', persisted='true'
D)
Parent constructor called, id = 10
age=15, name='abc', password='null', persisted='true'
E)
Parent constructor called, id = 10
Parent constructor called, id = 10
age=15, name='abc', password='null', persisted='false'
🔎 Explanation
A)
❌ No error occurs. Both writing and reading work fine because Child implements Serializable.
B)
❌ id = 2 is wrong: since Parent is not Serializable, the value 2 is not stored. The Parent constructor resets it to 10.
❌ persisted = true is wrong: the transient flag is reset to false after deserialization, and the constructor is not called.
❌ password = pass is wrong: the transient flag is reset to null.
C)
❌ persisted = true is wrong: although the no-arg constructor of Child sets it to true, the JVM does not call the subclass constructor during deserialization. Only the non-serializable parent constructor is called.
D)
❌ Only one parent constructor call appears here, but during deserialization, the Parent constructor will run again, as it is not Serializable, so we expect two prints.
E)
Correct ✅
Parent is not Serializable
SinceParentdoes not implementSerializable, its fields are not persisted in serialization.
During deserialization, the JVM calls the Parent’s no-arg constructor again, initializingid = 10.
That’s why we see two constructor calls in the output: once when creating the object, and once when reading it back.Child is Serializable
Fieldsageandnameare serialized. The valueage=15is restored (not 20, because we modified it before writing).Transient fields are not serialized
Bothpasswordandpersistedare transient. They reset to their default values (nullfor String,falsefor boolean).
✅ Correct Answer
E
Did you get it right? Don’t worry if you missed some; this is a hard question. The goal here is to expose you to several tricky details that often show up in the Java 21 certification exam.
⚡ Next in this series: we’ll dive into bitwise operations
📬 Stay tuned:
If you don’t want to miss the upcoming parts of this Java 21 Certification series, hit the Subscribe button below.


