Cloning Data Structures Flashcards

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

What is cloning in the context of data structures?

A

Creating a duplicate copy of an object and its data.

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

What is the simple case for cloning objects?

A

For objects with only primitive data, create a new object of the same type and copy the instance variables.

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

In complex cases of cloning, what must be considered?

A

Should referenced objects also be cloned?

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

Define immutable objects.

A

Data cannot be changed after creation (e.g., Java wrapper classes like Integer, Double).

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

Define mutable objects.

A

Data can be changed after creation (e.g., LinkedNode280<i> and ListList280<i>).</i></i>

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

What is a shallow clone?

A

Creates a new object that is a direct copy of the original object.

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

What is the process of a shallow clone?

A
  • Copies the object’s immediate instance variables into the new object. * Copies references to other objects without cloning them.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the result of a shallow clone?

A

The original and cloned objects share references to the same nested objects.

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

What issue arises from shallow cloning?

A

Changes to the nested objects in one list affect the other list.

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

Define deep clone.

A

Creates a fully independent copy of the object and all objects it references.

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

What is the process of a deep clone?

A
  • Performs a shallow clone of the object itself. * Recursively clones every object referenced by the original object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the result of a deep clone?

A

The original and cloned objects do not share any referenced objects.

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

What is a benefit of deep cloning?

A

Modifications to the cloned object do not affect the original object.

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

What is a tradeoff of deep cloning?

A

More time and memory are required to perform a deep clone.

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

What is the rule regarding immutable objects in deep cloning?

A

Immutable objects do not need to be cloned in a deep clone.

17
Q

Why do immutable objects not need to be cloned?

A

Immutable objects cannot be modified, so sharing references does not cause issues.

18
Q

What are the key differences between shallow and deep cloning?

A
  • Shallow Clone: Copies the object but shares references to nested objects. * Deep Clone: Fully duplicates the object and all referenced objects.
19
Q

What method in Java can create a shallow clone of an object?

A

The clone() method in the Object class.

20
Q

What must objects implement to allow cloning in Java?

A

The Cloneable interface.

21
Q

What happens if an object does not implement Cloneable when using clone()?

A

A CloneNotSupportedException is thrown.

22
Q

What is the first line in the clone method of LinkedNode<i>?</i>

A

public LinkedNode<i> clone() { return (LinkedNode<i>) super.clone(); }</i></i>

23
Q

What is the purpose of the try-catch block while cloning a list in Java?

A

To handle CloneNotSupportedException when cloning the list.

24
Q

What is the expected behavior when a node is removed from a cloned list?

A

The original list should remain unchanged.

25
What is the first step in the deep clone method for LinkedList?
LinkedList newList = (LinkedList) super.clone();
26
What does 'protected' mean in the context of the clone method?
It ensures that explicit permission is granted for shallow cloning.