Shallow and deep copies Flashcards

1
Q

Question: Consider a tuple as shown below:

Code Editor:
tuple_a = (“John”, “Amy”, “Jules”, [“Jim”, “Ana”])

What code would you run to create a deep copy of this tuple?

A

deep_copy = copy.deepcopy(tuple_a)

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

Question: Consider the following code:

Code Editor:
old_str = “Hello”
new_str = old_str
new_str = “World”

What will be the value stored in old_str after these lines of code are executed?

A

“Hello”

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

old_list = [“John”, “Amy”, “Jules”]
new_list = old_list.copy()
new_list.append(“Emily”)

What will be the value stored in old_list after these lines of code are executed?

A

[“John”, “Amy”, “Jules”]

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

Code Editor:
dict_a = {“John”: 35, “Jim”: 22, “Jill”: 44}
dict_copy = dict_a
dict_copy[‘John’] = 40
del dict_copy[‘John’]

After executing this code, what is the value associated with John in dict_a?

A

The key John does not exist in dict_a

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

For a set in Python what is the difference between copy.copy() and the copy.deepcopy() method?

A

No difference between the two because sets cannot hold mutable types like lists

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

old_list = [“John”, “Amy”, “Jules”]
new_list = old_list
new_list.append(“Emily”)

What will be the value stored in old_list after these lines of code are executed?

A

[“John”, “Amy”, “Jules”, “Emily”]

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

Code Editor:
dict_a = {“John”: 35, “Jim”: 22, “Jill”: 44}
dict_copy = dict_a.copy()
dict_copy[‘John’] = 40

After executing this code, what is the value associated with John in dict_a?

A

35

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