Python List Quiz


123
Question 1
Question 2: Find the output of the following program:
nameList = ['Harsh', 'Pratik', 'Bob', 'Dhruv'] 

print nameList[1][-1] 

A
r
B
b
C
D
D
k
Python List Quiz    
Discuss it


Question 1 Explanation: 
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
Question 3: Find the output of the following program:
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) 


A
[1,2,3,4,5,6,7,8]
B
[1,2,3,4]
C
[1,2,3,4,[5,6,7,8]]
D
[1,2,3,4][5,6,7,8]
Python List Quiz    
Discuss it


Question 2 Explanation: 
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
Question 4: Find the output of the following program:
def addToList(listcontainer): 
	listcontainer += [10] 

mylistContainer = [10, 20, 30, 40] 
addToList(mylistContainer) 
print len(mylistContainer) 

A
4
B
5
C
6
D
10
Python List Quiz    
Discuss it


Question 3 Explanation: 
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
Question: 5 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) 

A
4
B
5
C
11
D
12
Python List Quiz    
Discuss it


Question 4 Explanation: 
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 doest gets 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
Question 6: Find the output of the following program:
def gfg(x,l=[]): 
	for i in range(x): 
		l.append(i*i) 
	print(l) 

gfg(3,[3,2,1]) 

A
[3, 2, 1, 0, 1, 4]
B
[0, 1, 0, 1, 4]
C
[0, 1]
D
[ ]
Python List Quiz    
Discuss it


Question 5 Explanation: 
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
Question: 7 Find the output of the following program:
# statement 1
list1 = range(100, 110) 

# statement 2
print (list1.index(105))

A
105
B
5
C
106
D
104
Python List Quiz    
Discuss it


Question 6 Explanation: 
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
Question 8: Find the output of the following program:
list1 = [1, 2, 3, 4, 5] 
list2 = list1 

list2[0] = 0; 

print( list1)  

A
[1, 2, 3, 4, 5, 0]
B
[0,1, 2, 3, 4, 5]
C
[0, 2, 3, 4, 5]
D
[1, 2, 3, 4, 0]
Python List Quiz    
Discuss it


Question 7 Explanation: 
n 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
Question 9: Find the output of the following program:
list1 = [1998, 2002] 
list2 = [2014, 2016] 

print ((list1 + list2)*2)
A
[1998, 2002, 2014, 2016, 1998, 2002, 2014, 2016]
B
[1998, 2002, 2014, 2016]
C
[1998, 2002, 1998, 2002]
D
[2014, 2016, 2014, 2016]
Python List Quiz    
Discuss it


Question 8 Explanation: 
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
Question 10: Find the output of the following program:
list1 = ['physics', 'chemistry', 1997, 2000] 

print (list1[1][-1])

A
p
B
y
C
7
D
2
Python List Quiz    
Discuss it


Question 9 Explanation: 
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
Question 11: Find the output of the following program:
list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] 
print(len(list))

A
12
B
11
C
6
D
22
Python List Quiz    
Discuss it


Question 10 Explanation: 
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.
There are 24 questions to complete.
123
My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.