Open In App
Related Articles

Python-Quizzes | Python List Quiz | Question 5

Improve Article
Save Article
Like Article

Find the output of the following program: 


check1 = ['Learn', 'Quiz', 'Practice', 'Contribute']
check2 = check1
check3 = check1[:]
check2[0] = 'Code'
check3[1] = 'Mcq'
count = 0
for c in (check1, check2, check3):
    if c[0] == 'Code':
        count += 1
    if c[1] == 'Mcq':
        count += 10
print (count)









Answer: (D)


When assigning check1 to check2, we create a second reference to the same list. Changes to check2 affect check1. When assigning the slice of all elements in check1 to check3, we are creating a full copy of check1 which can be modified independently (i.e, any change in check3 will not affect check1). 
So, while checking check1 ‘Code’ gets matched and the count increases to 1, but Mcq doesn’t get matched since its available only in check3. 
Now checking check2 here also ‘Code’ gets matched resulting in a count value to 2. 
Finally while checking check3 which is separate from both check1 and check2 here only Mcq gets matched and the count becomes 12.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Last Updated : 17 Sep, 2020
Like Article
Save Article
Similar Reads