Serialization Flashcards

(141 cards)

1
Q

What is serialization in Java?

A

Serialization is the process of converting an object into a byte stream so it can be stored or transmitted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is serialization used?

A

To save object state to a file, send objects over a network, or cache objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does serialization include?

A

Class information, object state, and metadata needed to reconstruct the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What interface must a class implement to be serializable?

A

java.io.Serializable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is Serializable a marker interface?

A

Yes. It has no methods and simply marks a class as serializable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you serialize an object?

A

Using ObjectOutputStream.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Example of serialization code?

A

ObjectOutputStream oos = new ObjectOutputStream(out); oos.writeObject(obj);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is deserialization?

A

The process of converting a byte stream back into an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you deserialize an object?

A

Using ObjectInputStream.readObject().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What exception occurs if class is not serializable?

A

NotSerializableException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What keyword prevents a field from being serialized?

A

transient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why use transient fields?

A

To exclude sensitive or unnecessary data from serialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is serialVersionUID?

A

A unique version identifier for a serializable class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is serialVersionUID important?

A

It ensures compatibility between serialized objects and class versions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can static fields be serialized?

A

No. Static fields belong to class, not object state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Real-world example use of serialization?

A

Sending objects between client and server in distributed systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Key rule to remember?

A

Serialization = object → byte stream; Deserialization = byte stream → object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the purpose of serialization in Java?

A

Serialization allows objects to be converted into byte streams for storage, transmission, or later reconstruction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Why is serialization used for communication?

A

It enables objects to be sent over networks between different systems.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How does serialization support persistence?

A

It allows object state to be saved to files or databases and restored later.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Why is serialization useful for caching?

A

Serialized objects can be stored and quickly reloaded instead of recomputing them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How does serialization help performance?

A

Deserializing an object is often much faster than rebuilding it from scratch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is cross-JVM serialization use?

A

It allows objects to be transferred and used across different JVM environments or machines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What distributed systems benefit from serialization?

A

Client-server applications and microservices exchanging objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why is serialization useful in session management?
It lets session objects be stored and restored across server restarts.
26
Can serialization be used for deep cloning?
Yes. Objects can be serialized and deserialized to create deep copies.
27
Why is serialization important for remote communication APIs?
Because data must be converted to transferable format.
28
What role does serialization play in messaging systems?
It converts objects into transferable message payloads.
29
Is serialization useful for logging or auditing?
Yes. Object state snapshots can be stored for later inspection.
30
Does serialization help with failover systems?
Yes. State can be saved and restored after crashes.
31
What must be true for an object to be serialized?
Its class must implement Serializable.
32
Key rule to remember?
Serialization enables storage, transfer, caching, and reconstruction of objects.
33
What is deserialization in Java?
Deserialization is the process of reconstructing an object from its byte stream representation.
34
What is the opposite of serialization?
Deserialization.
35
What does deserialization restore?
The object's state, class information, and data values.
36
Which class is used for deserialization?
ObjectInputStream.
37
Example of deserialization code?
ObjectInputStream ois = new ObjectInputStream(in); Object obj = ois.readObject();
38
What method performs deserialization?
readObject().
39
What must be true for a class to be deserialized?
It must implement Serializable.
40
What happens if class definition changed after serialization?
It may cause InvalidClassException due to version mismatch.
41
What role does serialVersionUID play in deserialization?
It ensures compatibility between serialized data and class definition.
42
Can transient fields be restored during deserialization?
No. They are skipped and initialized with default values.
43
Can static fields be restored during deserialization?
No. Static fields belong to class, not object state.
44
Is constructor called during deserialization?
No. Object is created without calling constructors.
45
Why is deserialization powerful?
It allows objects to be reconstructed across systems or sessions.
46
Is deserialization safe by default?
No. It can be vulnerable if untrusted data is deserialized.
47
What security risk exists with deserialization?
Malicious byte streams can exploit classes and execute code.
48
Key rule to remember?
Deserialization = byte stream → object.
49
What is serialization conceptually?
Serialization is the process of converting an object’s state into a stream of bytes.
50
What is deserialization conceptually?
Deserialization is the process of reconstructing an object from a byte stream.
51
What is the core idea behind serialization?
Transforming in-memory objects into a transferable or storable format.
52
What is the core idea behind deserialization?
Rebuilding objects from stored or transmitted data.
53
What direction does serialization go?
Object → Byte stream.
54
What direction does deserialization go?
Byte stream → Object.
55
Why are serialization and deserialization used together?
They allow objects to be transmitted, stored, and later restored.
56
Do serialization and deserialization preserve object state?
Yes. They preserve field values and structure.
57
Are methods serialized?
No. Only object state is serialized, not behavior.
58
Is serialization a physical or conceptual transformation?
It is a conceptual data transformation process.
59
Real-world analogy for serialization?
Saving a game state to a file.
60
Real-world analogy for deserialization?
Loading the saved game and continuing from same point.
61
Can serialized data be stored long-term?
Yes. It can be saved to disk or database.
62
Can serialized data be sent across networks?
Yes. It is commonly used in distributed systems.
63
Key rule to remember?
Serialization saves object state; deserialization restores it.
64
Why do we mark a data member transient in Java?
To exclude that field from serialization so it is not saved in the byte stream.
65
What does the transient keyword mean?
It indicates a variable should not be persisted during serialization.
66
Why would you not want a field serialized?
Because it may contain sensitive, temporary, or non-serializable data.
67
What happens to transient fields during deserialization?
They are initialized with default values (null, 0, false).
68
Do transient fields belong to persistent object state?
No. They are treated as temporary runtime data.
69
Can transient fields be accessed normally at runtime?
Yes. They behave like normal fields except during serialization.
70
Is transient related to access modifiers?
No. It affects serialization behavior, not visibility.
71
Example of transient field usage?
transient String password;
72
Why mark passwords as transient?
To prevent sensitive data from being stored or transmitted.
73
Can static fields be transient?
They can be declared transient, but it has no effect because static fields are not serialized anyway.
74
Does transient affect cloning?
No. It only affects serialization.
75
Can transient variables be manually serialized?
Yes. By implementing custom writeObject/readObject methods.
76
What is a common use case for transient fields?
Cache values or derived data that can be recomputed.
77
Does transient improve performance?
It can reduce serialization size and time.
78
Key rule to remember?
transient = skip this field during serialization.
79
Can a method be declared transient in Java?
No. The transient keyword can only be applied to fields, not methods.
80
Why can’t methods be transient?
Because transient is related to serialization, which applies only to object state (fields), not behavior (methods).
81
Where is the transient keyword valid?
Only on instance variables.
82
What happens if you try to mark a method transient?
The compiler throws an illegal modifier error.
83
What does transient actually control?
It controls whether a field is included during serialization.
84
Do methods get serialized in Java?
No. Serialization stores only object data, not method code.
85
Why are methods excluded from serialization?
Because methods belong to the class definition, not the object’s runtime state.
86
Can constructors be transient?
No. Constructors are not fields.
87
Can classes be transient?
No. Only variables can be marked transient.
88
Can static variables be transient?
They can be declared so, but it has no effect because static fields are not serialized anyway.
89
Which keyword controls serialization of methods?
None. Methods are never serialized.
90
What keyword is often confused with transient?
volatile, which controls thread visibility.
91
Does transient affect runtime execution?
No. It only affects serialization behavior.
92
Key rule to remember?
transient applies only to fields, never methods.
93
How does marking a field transient make serialization possible?
Because transient fields are skipped during serialization, allowing the object to be serialized even if that field’s type is not serializable.
94
Why would serialization fail without transient?
If a field references a non-serializable object, serialization throws NotSerializableException.
95
How does transient prevent NotSerializableException?
It tells the JVM to ignore that field when converting the object to a byte stream.
96
Example scenario?
Class A implements Serializable but contains field B that isn’t serializable; marking B as transient allows A to serialize.
97
What happens to a transient field after deserialization?
It is restored with its default value.
98
Does transient remove the field permanently?
No. It only excludes it from serialization.
99
When should you mark a field transient?
When the field is temporary, sensitive, or non-serializable.
100
Does transient affect normal runtime use?
No. The field behaves normally until serialization occurs.
101
Can transient be used for performance optimization?
Yes. It reduces serialized object size.
102
What is the alternative to transient for non-serializable fields?
Make the referenced class implement Serializable.
103
Can transient fields be manually serialized?
Yes. By implementing custom writeObject() and readObject() methods.
104
Does transient affect cloning or copying objects?
No. It only affects serialization.
105
Why is transient useful for resources like sockets or threads?
Because such resources cannot be meaningfully serialized.
106
Is transient checked at compile time or runtime?
Runtime, during serialization process.
107
Key rule to remember?
transient = skip this field so serialization can succeed.
108
What is the Externalizable interface in Java?
Externalizable is an interface that allows a class to control its own serialization and deserialization process.
109
Which interface does Externalizable extend?
It extends Serializable.
110
Why use Externalizable instead of Serializable?
To gain full control over what data is saved and restored.
111
Which methods must be implemented when using Externalizable?
writeExternal(ObjectOutput out) and readExternal(ObjectInput in).
112
What does writeExternal() do?
It defines how the object’s state is written to a stream.
113
What does readExternal() do?
It defines how the object’s state is reconstructed from a stream.
114
Who controls serialization when Externalizable is used?
The programmer, not the JVM default mechanism.
115
Is default serialization used with Externalizable?
No. Default serialization is bypassed.
116
Must an Externalizable class have a constructor?
Yes. It must have a public no-argument constructor.
117
Why is a no-arg constructor required?
Because JVM uses it to create the object before calling readExternal().
118
Can you choose which fields to serialize with Externalizable?
Yes. You manually decide which fields to write.
119
Is Externalizable faster than Serializable?
It can be, because you can optimize what data is written.
120
Does Externalizable improve security?
Yes. You control exactly what data is serialized.
121
Is implementing Externalizable more complex?
Yes. It requires manual coding of serialization logic.
122
When should Externalizable be used?
When you need custom serialization logic or performance optimization.
123
Key rule to remember?
Serializable = automatic serialization; Externalizable = manual serialization control.
124
What is the difference between Serializable and Externalizable in Java?
Serializable provides automatic serialization, while Externalizable gives full manual control over serialization logic.
125
Is Serializable a marker interface?
Yes. It has no methods and simply marks a class as serializable.
126
Is Externalizable a marker interface?
No. It requires implementing specific methods.
127
Which methods are required for Externalizable?
writeExternal() and readExternal().
128
Does Serializable require method implementation?
No. Serialization works automatically unless customized.
129
Can Serializable be customized?
Yes. By defining private writeObject() and readObject() methods.
130
Who controls serialization in Serializable?
The JVM by default.
131
Who controls serialization in Externalizable?
The programmer.
132
Is a no-argument constructor required for Serializable?
No.
133
Is a no-argument constructor required for Externalizable?
Yes. It must be public.
134
Which interface gives better performance control?
Externalizable, because you control exactly what is serialized.
135
Which interface is easier to implement?
Serializable, because it requires no methods.
136
What happens if you forget to serialize a field in Externalizable?
It will not be saved, since serialization is manual.
137
Does Serializable use a recursive mechanism?
Yes. It automatically serializes referenced objects if they are serializable.
138
What is serialVersionUID used for?
To verify compatibility between serialized objects and class versions.
139
Is serialVersionUID required?
Not mandatory, but recommended to avoid compatibility issues.
140
Which is safer for sensitive data?
Externalizable, because you explicitly choose what to serialize.
141
Key rule to remember?
Serializable = automatic; Externalizable = manual control.