Informatics Practices
Consider the following code:
>>>A = [10, 20, 30, 40]
>>>B = A.copy()
>>>A[2] = 50
>>>B
Which of the following will be the elements of list A and B?
- A = [10, 20, 50, 40]
B = [10, 20, 30, 40] - A = [10, 20, 30, 40]
B = [10, 20, 50, 40] - A = [10, 20, 30, 40, 50]
B = [10, 20, 50, 40] - A = [10, 20, 40]
B = [10, 20, 50, 40]
Answer
A=[10, 20, 50, 40]
B=[10, 20, 30, 40]
Reason — The code initializes list A with elements [10, 20, 30, 40]. It then creates a copy of list A named list B using the copy() method. Subsequently, the third element of list A is modified to 50 using A[2] = 50. At this point, B is still [10, 20, 30, 40], because it's a separate list from A. The copy() method does not modify the original list or the modifications made in the new list will not be reflected to the base list.
Related Questions
The sequential accessing of each of the elements in a list is called:
- List Indexing
- List Traversal
- List Slicing
- List Accessing
Consider the following lists:
L1 = ["this", "is", "a", "list"] L2 = ["this", "is", "another list"]Which of the following statements will result in an error?
- L1 = L2
- L1.copy()
- L2.append(1, 2, 3)
- L2.pop()
Consider the following lists:
A = [1,2] B = [1,2]What will be the output of the following?
print(A==B)- True
- False
- Error
- No output
Which of the following functions will return the first occurrence of the specified element in a list?
- sort()
- value()
- index()
- Sorted()