Test Series - python

Test Number 72/108

Q: Which type of copy is shown in the following python code?

l1=[[10, 20], [30, 40], [50, 60]]
ls=list(l1)
ls
[[10, 20], [30, 40], [50, 60]]
A. Shallow copy
B. Deep copy
C. memberwise
D. All of the mentioned
Solution: The code shown above depicts shallow copy. For deep copy, the command given is: l2 = l1.copy().
Q: What will be the output of the following Python code?

l=[2, 3, [4, 5]]
l2=l.copy()
l2[0]=88
l
l2
A. [88, 2, 3, [4, 5]] [88, 2, 3, [4, 5]]
B. [2, 3, [4, 5]] [88, 2, 3, [4, 5]]
C. [88, 2, 3, [4, 5]] [2, 3, [4, 5]]
D. [2, 3, [4, 5]] [2, 3, [4, 5]]
Solution: The code shown above depicts deep copy. In deep copy, the base address of the objects is not copied. Hence the modification done on one list does not affect the other list.
Q: In _______________ copy, the base address of the objects are copied. In _______________ copy, the base address of the objects are not copied.
A. deep. shallow
B. memberwise, shallow
C. shallow, deep
D. deep, memberwise
Solution: In shallow copy, the base address of the objects are copied.
In deep copy, the base address of the objects are not copied.
Note that memberwise copy is another name for shallow copy.
Q: The nested list undergoes shallow copy even when the list as a whole undergoes deep copy.
A. True
B. False
C. none
D. ..
Solution: A nested list undergoes shallow copy even when the list as a whole undergoes deep copy. Hence, this statement is true.
Q: What will be the output of the following Python code and state the type of copy that is depicted?

l1=[2, 4, 6, 8]
l2=[1, 2, 3]
l1=l2
l2
A. [2, 4, 6, 8], shallow copy
B. [2, 4, 6, 8], deep copy
C. [1, 2, 3], shallow copy
D. [1, 2, 3], deep copy
Solution: The code shown above depicts shallow copy and the output of the code is: [1, 2, 3].
Q: What will be the output of the following Python code?

l1=[10, 20, 30]
l2=l1
id(l1)==id(l2)
 
l2=l1.copy()
id(l1)==id(l2)
A. False, False
B. False, True
C. True, True
D. True, False
Solution: The first code shown above represents shallow copy. Hence the output of the expression id(l1)==id(l2) is True. The second code depicts deep copy. Hence the output of the expression id(l1)==id(l2) in the second case is False.
Q: What will be the output of the following Python code?

l1=[1, 2, 3, [4]]
l2=list(l1)
id(l1)==id(l2)
A. True
B. False
C. Error
D. Address of l1
Solution: The code shown above shows a nested list. A nested list will undergo shallow copy when the list as a whole undergoes deep copy. Hence the output of this code is False.
Q: What will be the output of the following Python code?

l1=[10, 20, 30, [40]]
l2=copy.deepcopy(l1)
l1[3][0]=90
l1
l2
A. [10, 20, 30, [40]] [10, 20, 30, 90]
B. Error
C. [10, 20, 30 [90]] [10, 20, 30, [40]]
D. [10, 20, 30, [40]] [10, 20, 30, [90]]
Solution: The code shown above depicts deep copy. Hence at the end of the code, l1=[10, 20, 30, [90]] and l2=[10, 20, 30, [40]].
Q: In ____________________ copy, the modification done on one list affects the other list. In ____________________ copy, the modification done on one list does not affect the other list.
A. shallow, deep
B. memberwise, shallow
C. deep, shallow
D. deep, memberwise
Solution: In shallow copy, the modification done on one list affects the other list. In deep copy, the modification done on one list does not affect the other list.
Q: What will be the output of the following Python code?

l1=[1, 2, 3, (4)]
l2=l1.copy()
l2
l1
A. [1, 2, 3, (4)] [1, 2, 3, 4]
B. [1, 2, 3, 4] [1, 2, 3, (4)]
C. [1, 2, 3, 4] [1, 2, 3, 4]
D. [1, 2, 3, (4)] [1, 2, 3, (4)]
Solution: In the code shown above, the list l1 is enclosed in a tuple. When we print this list, it is printed as [1, 2, 3, 4]. Note the absence of the tuple. The code shown depicts deep copy. Hence the output of this program is: l1=[1, 2, 3, 4] and l2=[1, 2, 3, 4].

You Have Score    /10