Python List Quiz
Question 1 |
Find the output of the following program:
Python3
nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] print (nameList[1][-1])
r | |
b | |
D | |
k |
Discuss it
The index position -1 represents either the last element in a list or the last character in a String. In the above given list of names โnameListโ, the index 1 represents the second element i.e, the second string โPratikโ and the index -1 represents the last character in the string โPratikโ. So, the output is โkโ.
Question 2 |
Find the output of the following program:
Python3
geekCodes = [1, 2, 3, 4] # List will look like as [1,2,3,4,[5,6,7,8]] geekCodes.append([5,6,7,8]) print(geekCodes)
[1,2,3,4,5,6,7,8] | |
[1,2,3,4] | |
[1,2,3,4,[5,6,7,8]] | |
[1,2,3,4][5,6,7,8] |
Discuss it
The task of the append() method is to append a passed obj into an existing list. But instead of passing a list to the append method will not merge the two lists, the entire list which is passed is added as an element of the list.
Question 3 |
Find the output of the following program:
Python3
def addToList(listcontainer): listcontainer += [10] mylistContainer = [10, 20, 30, 40] addToList(mylistContainer) print (len(mylistContainer))
4 | |
5 | |
6 | |
10 |
Discuss it
In Python, everything is a reference, and references are passed by value. Parameter passing in Python is the same as reference passing in Java. As a consequence, the function can modify the value referred by passed argument, i.e. the value of the variable in the callerโs scope can be changed. Here the task of the function โaddToListโ is to add an element 10 in the list, So this will increase the length of the list by 1. So the output of the program is 5.
Question 4 |
Find the output of the following program:
Python3
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)
4 | |
5 | |
11 | |
12 |
Discuss it
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.
Question 5 |
Find the output of the following program:
Python3
def gfg(x,l=[]): for i in range(x): l.append(i*i) print(l) gfg(3,[3,2,1])
[3, 2, 1, 0, 1, 4] | |
[0, 1, 0, 1, 4] | |
[0, 1] | |
[ ] |
Discuss it
l is a name for a variable that points to a list stored in memory. The function call starts off by creating a new list in a new block of memory. l then refers to this new list. It then appends 0, 1, and 4 to this new list. So thatโs great.
Question 6 |
Find the output of the following program:
Python3
# statement 1 list1 = range(100, 110) # statement 2 print (list1.index(105))
105 | |
5 | |
106 | |
104 |
Discuss it
Statement 1: will genetrate numbers from 100 to 110 and appent all these numbers in the list. Statement 2: will give the index value of 105 in the list list1.
Question 7 |
Find the output of the following program:
Python3
list1 = [1, 2, 3, 4, 5] list2 = list1 list2[0] = 0; print( list1)
[1, 2, 3, 4, 5, 0] | |
[0,1, 2, 3, 4, 5] | |
[0, 2, 3, 4, 5] | |
[1, 2, 3, 4, 0] |
Discuss it
In this problem, we have provided a reference to the list1 with another name list2 but these two lists are same which have two references(list1 and list2). So any alteration with list2 will affect the original list.
Question 8 |
Find the output of the following program:
Python3
list1 = [1998, 2002] list2 = [2014, 2016] print ((list1 + list2)*2)
[1998, 2002, 2014, 2016, 1998, 2002, 2014, 2016] | |
[1998, 2002, 2014, 2016] | |
[1998, 2002, 1998, 2002] | |
[2014, 2016, 2014, 2016] |
Discuss it
When the addition(+) operator uses a list as its operands then the two lists will get concatenated. And when a list id multiplied with a constant k>=0 then the same list is appended k times in the original list.
Question 9 |
Find the output of the following program:
Python3
list1 = ['physics', 'chemistry', 1997, 2000] print (list1[1][-1])
p | |
y | |
7 | |
2 |
Discuss it
In python, we can slice a list but we can also slice an element within the list if it is a string. The declaration list[x][y] will mean that โxโ is the index of the element within a list and โyโ is the index of an entity within that string.
Question 10 |
Find the output of the following program:
Python3
list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print(len(list))
12 | |
11 | |
6 | |
22 |
Discuss it
The beauty of python list datatype is that within a list, a programmer can nest another list, a dictionary, or a tuple. Since in the code, there are 6 items present in the list the length of the list is 6.