0% found this document useful (0 votes)
45 views6 pages

5 MCQ Class 11th LIST

The document contains multiple-choice questions (MCQs) related to Python lists, including operations such as creating lists, manipulating elements, and using built-in functions. Each question is followed by possible answers and a solution section that provides the correct answer along with explanations. The content is structured in a quiz format, aimed at testing knowledge of Python list functionalities.

Uploaded by

Palak Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

5 MCQ Class 11th LIST

The document contains multiple-choice questions (MCQs) related to Python lists, including operations such as creating lists, manipulating elements, and using built-in functions. Each question is followed by possible answers and a solution section that provides the correct answer along with explanations. The content is structured in a quiz format, aimed at testing knowledge of Python list functionalities.

Uploaded by

Palak Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MCQ PYTHON LIST By Gajendra Sir Mo. No.

: 9810301034
Python Questions and Answers – Lists – 1
1. Which of the following commands will create a list? 6. Suppose list1 is [1, 5, 9], what is sum(list1) ?
a) list1 = list() b) list1 = []. a) 1 b) 9 c) 15 d) Error
c) list1 = list([1, 2, 3]) d) all of the mentioned 7. To shuffle the list(say list1) what function do we use ?
2. What is the output when we execute list(“hello”)? a) list1.shuffle() b) shuffle(list1)
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]. b) [‘hello’]. c) random.shuffle(list1) d) random.shuffleList(lit1)
c) [‘llo’]. d) [‘olleh’]. 8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following
3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is is correct syntax for slicing operation ?
len(listExample)? a) print(list1[0]) b) print(list1[:2])
a) 5 b) 4 c) None d) Error c) print(list1[:-2]) d) all of the mentioned
4. Suppose list1 is [2445,133,12454,123], what is max(list1) ? 9. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
a) 2445 b) 133 c) 12454 d) 123 a) Error b) None c) 25 d) 2
5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ? 10. Suppose list1 is [2, 33, 222, 14, 25],What is list1[:-1]?
a) 3 b) 5 c) 25 d) 1 a) [2, 33, 222, 14]. b) Error c) 25 d) [25, 14, 222, 33, 2].

SOLUTION

Ans 1. d Execute in the shell to verify Ans 7. c Execute in the shell to verify .
Ans 2. a Execute in the shell to verify. Ans 8. d Slicing is allowed in lists just as in the case of
Ans 3. a Execute in the shell and verify. strings.
Ans 4. c Max returns the maximum element in the list. Ans 9. c -1 corresponds to the last index in the list.
Ans 5. d Min returns the minimum element in the list. Ans 10. a Execute in the shell to verify.
Ans 6. c Sum returns the sum of all elements in the list.

Python Questions and Answers – Lists – 2


1. What is the output when following code is executed ? 5. What is the output when following code is executed ?
>>>names = ['Amir', 'Bear', 'Charlton', 'Daman'] >>>list1 = [11, 2, 23]
>>>print(names[-1][-1]) >>>list2 = [11, 2, 2]
a) A b) Daman c) Error d) n >>>list1 < list2 is
2. What is the output when following code is executed ? a) True b) False c) Error d) None
names1 = ['Amir', 'Bear', 'Charlton', 'Daman'] 6. To add a new element to a list we use which command ?
names2 = names1; names3 = names1[:] a) list1.add(5) b) list1.append(5)
names2[0] = 'Alice' ; names3[1] = 'Bob' c) list1.addLast(5) d) list1.addEnd(5)
sum = 0 7. To insert 5 to the third position in list1, we use which
for ls in (names1, names2, names3): command ?
if ls[0] == 'Alice': a) list1.insert(3, 5) b) list1.insert(2, 5)
sum += 1 c) list1.add(3, 5) d) list1.append(3, 5)
if ls[1] == 'Bob': 8. To remove string “hello” from list1, we use which command
sum += 10 ?
print (sum) a) list1.remove(“hello”) b) list1.remove(hello)
a) 11 b) 12 c) 21 d) 22 c) list1.removeAll(“hello”) d) list1.removeOne(“hello”)
3. Suppose list1 is [1, 3, 2], What is list1 * 2 ? 9. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5) ?
a)[2, 6, 4] b)[1, 3, 2, 1, 3]. c)[1, 3, 2, 1, 3, 2] d) [1,3,2, 3,2,1]. a) 0 b) 1 c) 4 d) 2
4. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is : 10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is
a) [0, 1, 2, 3]. b) [0, 1, 2, 3, 4]. list1.count(5) ?
c) [0.0, 0.5, 1.0, 1.5]. d) [0.0, 0.5, 1.0, 1.5, 2.0]. a) 0 b) 4 c) 1 d) 2

SOLUTION
Ans 1. d Execute in the shell to verify. Ans 5. b Elements are compared one by one.
Ans 2. b When assigning names1 to names2, we create a Ans 6. b We use the function append to add an element to
second reference to the same list. Changes to names2 affect the list.
names1. When assigning the slice of all elements in names1 Ans 7. a Execute in the shell to verify.
to names3, we are creating a full copy of names1 which can Ans 8. d Execute help(list.index) to get details.
be modified independently. Ans 9. 0
Ans 3. c Execute in the shell and verify. Ans 10. d Execute in the shell to verify.
Ans 4. c Execute in the shell to verify.

Page 1 of 6 Computer Science 083 PYTHON [email protected]


MCQ PYTHON LIST By Gajendra Sir Mo. No. : 9810301034
Python Questions and Answers – Lists – 3
1. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after max = myList[0]
list1.reverse() ? indexOfMax = 0
a) [3, 4, 5, 20, 5, 25, 1, 3]. b) [1, 3, 3, 4, 5, 5, 20, 25]. for i in range(1, len(myList)):
c) [25, 20, 5, 5, 4, 3, 3, 1]. d) [3, 1, 25, 5, 20, 5, 4, 3]. if myList[i] > max:
2. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 max = myList[i]
after listExample.extend([34, 5]) ? indexOfMax = i
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]. b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]. >>>print(indexOfMax)
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]. d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]. a) 1 b) 2 c) 3 d) 4
3. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 8. What is the output when following code is executed ?
after listExample.pop(1) ? myList = [1, 2, 3, 4, 5, 6]
a) [3, 4, 5, 20, 5, 25, 1, 3]. b) [1, 3, 3, 4, 5, 5, 20, 25]. for i in range(1, 6):
c) [3, 5, 20, 5, 25, 1, 3]. d) [1, 3, 4, 5, 20, 5, 25]. myList[i - 1] = myList[i]
the position specified in the parameter. for i in range(0, 6):
4. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 print(myList[i], end = " ")
after listExample.pop()? a) 2 3 4 5 6 1 b) 6 1 2 3 4 5 c) 2 3 4 5 6 6 d) 1 1 2 3 4 5
a) [3, 4, 5, 20, 5, 25, 1]. b) [1, 3, 3, 4, 5, 5, 20, 25]. 9. What is the output when following code is executed ?
c) [3, 5, 20, 5, 25, 1, 3]. d) [1, 3, 4, 5, 20, 5, 25]. >>>list1 = [1, 3]
5. What is the output when the following code is executed ? >>>list2 = list1
>>>"Welcome to Python".split() >>>list1[0] = 4
a) [“Welcome”, “to”, “Python”]. b) (“Welcome”, “to”, >>>print(list2)
“Python”) a) [1, 3]. b) [4, 3].
c) {“Welcome”, “to”, “Python”} d) “Welcome”, “to”, c) [1, 4]. d) [1, 3, 4].
“Python” 10. What is the output when following code is executed ?
6. What is the output when following code is executed ? def f(values):
>>>list("a#b#c#d".split('#')) values[0] = 44
a) [‘a’, ‘b’, ‘c’, ‘d’]. b) [‘a b c d’]. v = [1, 2, 3]
c) [‘a#b#c#d’]. d) [‘abcd’]. f(v)
7. What is the output when following code is executed ? print(v)
myList = [1, 5, 5, 5, 5, 1] a) [1, 44]. b) [1, 2, 3, 44]. c) [44, 2, 3]. d) [1, 2, 3].

SOLUTION
Ans 1. d Execute in the shell to verify. Ans 6. a Execute in the shell to verify.
Ans 2. a Execute in the shell to verify. Ans 7. a First time the highest number is encountered is at
Ans 3. c xplanation: pop() removes the element at index 1.
Ans 4. a pop() by default will remove the last element. Ans 8. c Execute in the shell to verify.
Ans 5. a split() function returns the elements in a list. Ans 9. b Lists should be copied by executing [:] operation.
Ans 10. c Execute in the shell to verify.

Python Questions and Answers – Lists – 4


1. What will be the output? a) None b) a c) b d) c
def f(i, values = []): 4. What will be the output?
values.append(i) numbers = [1, 2, 3, 4]
return values numbers.append([5,6,7,8])
f(1); f(2) print(len(numbers))
v = f(3) a) 4 b) 5 c) 8 d) 12
print(v) 5. To which of the following the “in” operator can be used to
a) [1] [2] [3]. b) [1] [1, 2] [1, 2, 3]. c) [1, 2, 3] d) 1 2 3 check if an item is in it?
2. What will be the output? a) Lists b) Dictionary c) Set d) All of the mentioned
names1 = ['Amir', 'Bala', 'Chales'] 6. What will be the output?
if 'amir' in names1: print(1) list1 = [1, 2, 3, 4]; list2 = [5, 6, 7, 8]
else: print(2) print(len(list1 + list2))
a) None b) 1 c) 2 d) Error a) 2 b) 4 c) 5 d) 8
3. What will be the output? 7. What will be the output?
names1 = ['Amir', 'Bala', 'Charlie'] def addItem(listParam):
names2 = [name.lower() for name in names1] listParam += [1]
print(names2[2][0]) mylist = [1, 2, 3, 4]

Page 2 of 6 Computer Science 083 PYTHON [email protected]


MCQ PYTHON LIST By Gajendra Sir Mo. No. : 9810301034
addItem(mylist) result.append(L[i])
print(len(mylist)) i=i+3
a) 1 b) 4 c) 5 d) 8 return result
8. What will be the output? a) Return a list containing every third item from L starting at
def increment_items(L, increment): index 0
i=0 b) Return an empty list
while i < len(L): c) Return a list containing every third index from L starting at
L[i] = L[i] + increment index 0
i=i+1 d) Return a list containing the items from L starting from index
values = [1, 2, 3] 0, omitting every third item
print(increment_items(values, 2)) 10. What will be the output?
print(values) veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
a) None [3, 4, 5]. b) None [1, 2, 3]. veggies.insert(veggies.index('broccoli'), 'celery')
c) [3, 4, 5]. [1, 2, 3]. d) [3, 4, 5]. None print(veggies)
9. What will be the output? a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’]
def example(L): Correct 1.00
''' (list) -> list ''' b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’].
i=0 c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’].
result = [] d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’].
while i < len(L):
SOLUTION
Ans 1. c Execute in the shell to verify Ans 6. d + appends all the elements individually into a
Ans 2. c Execute in the shell to verify. new list.
Ans 3. d List Comprehension are a shorthand for creating Ans 7. c + will append the element to the list.
new lists. Ans 8. a Execute in the shell to verify.
Ans 4. b A list is passed in append so the length is 5. Ans 9. a Run the code to get a better understanding with
Ans 5. a Lists many arguments.
Ans 10. a Execute in the shell to verify.

Python Questions and Answers – Lists – 5


1. What will be the output? for row in values:
>>>m = [[x, x + 1, x + 2] for x in range(0, 3)] row.sort()
a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]. for element in row:
c) [1, 2, 3, 4, 5, 6, 7, 8, 9]. d) [0, 1, 2, 1, 2, 3, 2, 3, 4]. print(element, end = " ")
2. How many elements are in m? print()
m = [[x, y] for x in range(0, 4) for y in range(0, 4)] a) The program prints two rows 3 4 5 1 followed by 33 6 1 2
a) 8 b) 12 c) 16 d) 32 b) The program prints on row 3 4 5 1 33 6 1 2
3. What will be the output? c) The program prints two rows 3 4 5 1 followed by 33 6 1 2
values = [[3, 4, 5, 1], [33, 6, 1, 2]] d) The program prints two rows 1 3 4 5 followed by 1 2 6 33
v = values[0][0] 6. What is the output?
for row in range(0, len(values)): matrix = [[1,2,3,4], [4,5,6,7], [8, 9, 10, 11], [12,13,14,15]]
for column in range(0, len(values[row])): for i in range(0, 4):
if v < values[row][column]: print(matrix[i][1], end = " ")
v = values[row][column] a) 1 2 3 4 b) 4 5 6 7 c) 1 3 8 12 d) 2 5 9 13
print(v) 7. What will be the output?
a) 3 b) 5 c) 6 d) 33 def m(list):
4. What will be the output? v = list[0]
values = [[3, 4, 5, 1], [33, 6, 1, 2]] for e in list:
v = values[0][0] if v < e: v = e
for lst in values: return v
for element in lst: values = [[3, 4, 5, 1], [33, 6, 1, 2]]
if v > element: for row in values:
v = element print(m(row), end = " ")
print(v) a) 3 33 b) 1 1 c) 5 6 d) 5 33
a) 1 b) 3 c) 5 d) 6 8. What will be the output?
5. What will be the output? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] ; print(data[1][0][0])
values = [[3, 4, 5, 1 ], [33, 6, 1, 2]] a) 1 b) 2 c) 4 d) 5

Page 3 of 6 Computer Science 083 PYTHON [email protected]


MCQ PYTHON LIST By Gajendra Sir Mo. No. : 9810301034
9. What will be the output? print(ttt(data[0]))
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] a) 1 b) 2 c) 4 d) 5
def ttt(m): 10. What will be the output?
v = m[0][0] points = [[1, 2], [3, 1.5], [0.5, 0.5]]
for row in m: points.sort(); print(points)
for element in row: a) [[1, 2], [3, 1.5], [0.5, 0.5]]. b) [[3, 1.5], [1, 2], [0.5, 0.5]].
if v < element: v = element c) [[0.5, 0.5], [1, 2], [3, 1.5]]. d) [[0.5, 0.5], [3, 1.5], [1, 2]].
return v

SOLUTION
Ans 1. b Execute in the shell to verify. Ans 6. d Execute in the shell to verify.
Ans 2. c Execute in the shell to verify. Ans 7. d Execute in the shell to verify.
Ans 3. d Execute in the shell to verify Ans 8. d Execute in the shell to verify.
Ans 4. a Execute in the shell to verify. Ans 9. c Execute in the shell to verify.
Ans 5. d Execute in the shell to verify. Ans 10. c Execute in the shell to verify.

Python Questions and Answers – Lists – 6


print(b)
1. What is the output of the following code? a) [‘a’,’b’,’c’,’d’]. , [‘a’,’b’,’c’,’d’].
a=[10,23,56,[78]] b) [‘a’,’@’,’b’,’@’,’c’,’@’,’d’]., [‘a’,’b’,’c’,’d’].
b=list(a) c) [‘a’,’@’,’b@c@d’]., [‘a’,’b’,’c’,’d’].
a[3][0]=95 d) [‘a’,’@’,’b@c@d’]., [‘a’,’@’,’b’,’@’,’c’,’@’,’d’].
a[1]=34 5. What is the output of the following code?
print(b) a=[1,2,3,4];b=[sum(a[0:x+1]) for x in range(0,len(a))];print(b)
a) [10,34,56,[95]]. b) [10,23,56,[78]]. a)10 b)[1,3,5,7]. c)4 d)[1,3,6,10].
c) [10,23,56,[95]]. d) [10,34,56,[78]]. 6. What is the output of the following code?
2. What does the following piece of code do? a="hello"; b=list((x.upper(),len(x)) for x in a); print(b)
print(list(zip((1,2,3),('a'),('xxx','yyy')))) a) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1)].
print(list(zip((2,4),('b','c'),('yy','xx')))) b) [(‘HELLO’, 5)].
a) [(1,2,3),(‘a’),(‘xxx’,’yyy’)]. c) [(‘H’, 5), (‘E’, 5), (‘L’, 5), (‘L’, 5), (‘O’, 5)].
[(2,4),(‘b’,’c’),(‘yy’,’xx’)]. d) Syntax error
b) [(1, ‘a’, ‘xxx’),(2,’ ‘,’yyy’),(3,’ ‘,’ ‘)]. 7. What is the output of the following code?
[(2, ‘b’, ‘yy’), (4, ‘c’, ‘xx’)]. a=[1,2,3,4]; b=[sum(a[0:x+1]) for x in range(0,len(a))]
c) Syntax error. print(b)
d) [(1, ‘a’, ‘xxx’)]. a) 10 b) [1,3,5,7]. c) 4 d) [1,3,6,10].
[(2, ‘b’, ‘yy’), (4, ‘c’, ‘xx’)]. 8. What is the output of the following code?
3. What is the output of the following code? a=[[]]*3; a[1].append(7) ; print(a)
import copy a) Syntax error b) [[7], [7], [7]].
a=[10,23,56,[78]] c) [[7], [], []]. d) [[],7, [], []].
b=copy.deepcopy(a) 9. What is the output of the following code?
a[3][0]=95 b=[2,3,4,5]
a[1]=34 a=list(filter(lambda x:x%2,b))
print(b) print(a)
a) [10,34,56,[95]]. b) [10,23,56,[78]]. a)[2,4]. b)[ ]. c)[3,5]. d)Invalid arguments for filter function
c) [10,23,56,[95]]. d) [10,34,56,[78]]. 10. What is the output of the following code?
4. What is the output of the following piece of code? lst=[3,4,6,1,2]
s="a@b@c@d" lst[1:2]=[7,8]
a=list(s.partition("@")) print(lst)
print(a) a)[3, 7, 8, 6, 1, 2]. b)Syntax error
b=list(s.split("@",3)) c)[3,[7,8],6,1,2]. d)[3,4,6,7,8].

SOLUTION
Ans 1. c The above copy is a type of shallow copy and only changes made in sublist is reflected in the copied list.
Ans 2. d The zip function combines the individual attributes of the lists into a list of tuples.
Ans 3. b The above copy is deepcopy. Any change made in the original list isn’t reflected.
Ans 4. c The partition function only splits for the first parameter along with the separator while split function splits for the
number of times given in the second argument but without the separator.
Page 4 of 6 Computer Science 083 PYTHON [email protected]
MCQ PYTHON LIST By Gajendra Sir Mo. No. : 9810301034
Ans 5. d The above code returns the cumulative sum of elements in a list.
Ans 6. a Variable x iterates over each letter in string a hence the length of each letter is 1.
Ans 7. d The above code returns the cumulative sum of elements in a list.
Ans 8. b The first line of the code creates multiple reference copies of sublist. Hence when 7 is appended, it gets appended
to all the sublists.
Ans 9. c The filter function gives value from the list b for which the condition is true, that is, x%2==1.
Ans 10. a In the piece of code, slice assignment has been implemented. The sliced list is replaced by the assigned elements in
the list. Type in python shell to verify.

Python Questions and Answers – Lists – 7


1. What is the output of the following code? a)[1] [2]. b)[49] [50]. c) Syntax error d )[[1]] [[2]].
a=[1,2,3]; b=a.append(4); print(a,’,’,b) 10. What is the output of the following code?
a) [1,2,3,4], [1,2,3,4]. b) [1, 2, 3, 4], None a=165; b=sum(list(map(int,str(a))));print(b)
c) Syntax error, d) [1,2,3], [1,2,3,4]. a) 561 b) 5 c) 12 d) Syntax error
2. What will be the output when executed in python shell? 11. What is the output of the following code?
a=[14,52,7]; b=a.copy(); b is a a= [1, 2, 3, 4, 5]
a) True b) False for i in range(1, 5):
3. What is the output of the following code? a[i-1] = a[i]
a=[13,56,17]; a.append([87]); a.extend([45,67]); print(a) for i in range(0, 5):
a)[13, 56, 17, [87], 45, 67]. b)[13, 56, 17, 87, 45, 67]. print(a[i],end = " ")
c)[13, 56, 17, 87,[ 45, 67]]. d)[13, 56, 17, [87], [45, 67]]. a)5 5 1 2 3 b)5 1 2 3 4 c)2 3 4 5 1 d)2 3 4 5 5
4. What is the output of the following piece of code? 12. What is the output of the following code?
a=list((45,)*4); print((45)*4); print(a) def change(var, lst):
a) 180, [(45),(45),(45),(45)]. b) (45,45,45,45). ,[45,45,45,45]. var = 1
c) 180 , [45,45,45,45]. d) Syntax error lst[0] = 44
5. What is the output of the following code? k = 3; a = [1, 2, 3]
lst=[[1,2],[3,4]]; print(sum(lst,[])) change(k, a)
a)[[3],[7]]. b)[1,2,3,4]. c)Error d)[10]. print(k); print(a)
6. What is the output of the following code? a)3[44, 2, 3].b)1[1,2,3]. c)3[1,2,3]. d)1 [44,2,3].
word1="Apple";word2="Apple"; 13. What is the output of the following code?
list1=[1,2,3]; list2=[1,2,3] a = [1, 5, 7, 9, 9, 1]
print(word1 is word2); print(list1 is list2) <br class="blank" />b=a[0]
a)True True b)False True c)False False d)True False <br class="blank" />x= 0
7. What is the output of the following code? for x in range(1, len(a)):
def unpack(a,b,c,d): if a[x] > b:
print(a+d) b = a[x]
x = [1,2,3,4] b= x
unpack(*x) print(b)
a)Error b)[1,4]. c)[5]. d)5 a)5 b)3 c)4 d)0
8. What is the output of the following code? 14. What is the output of the following code?
places = ['Bangalore', 'Mumbai', 'Delhi'] a=["Apple","Ball","Cobra"]
<br class="blank" />places1 = places <br class="blank" />a.sort(key=len)
places2 = places[:] print(a)
<br class="blank" />places1[1]="Pune" a)[‘Apple’, ‘Ball’, ‘Cobra’]. b)[‘Ball’, ‘Apple’, ‘Cobra’].
places2[2]="Hyderabad" c)[‘Cobra’, ‘Apple’, ‘Ball’]. d) Invalid syntax for sort().
print(places) 15. What is the output of the following code?
a)[‘Bangalore’, ‘Pune’, ‘Hyderabad’]. num = ['One', 'Two', 'Three']
b)[‘Bangalore’, ‘Pune’, ‘Delhi’]. for i, x in enumerate(num):
c)[‘Bangalore’, ‘Mumbai’, ‘Delhi’]. print('{}: {}'.format(i, x),end=" ")
d)[‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]. a)1: 2: 3: b) Exception is thrown
9. What is the output of the following piece of code? c)One Two Three d) 0: One 1: Two 2: Three
x=[[1],[2]]; print(" ".join(list(map(str,x))))

SOLUTION
Ans 1. b Append function on lists doesn’t return anything. Ans 2. b List b is just a copy of the original list. Any copy
Thus the value of b is None. made in list b will not be reflected in list a.

Page 5 of 6 Computer Science 083 PYTHON [email protected]


MCQ PYTHON LIST By Gajendra Sir Mo. No. : 9810301034
Ans 3. a The append function simply adds its arguments to the Ans 10. c First, map converts the number to string and then
list as it is while extend function extends its arguments and later places the individual digits in a list. Then, sum finds the sum of
appends it. the digits in the list. The code basically finds the sum of digits in
Ans 4. c (45) is an int while (45,) is a tuple of one element. the number.
Thus when a tuple is multiplied, it created references of itself Ans 11. d The items having indexes from 1 to 4 are shifted
which is later converted to a list. forward by one index due to the first for-loop and the item of
Ans 5. ….. index four is printed again because of the second for-loop.
Ans 6. d In the above case, both the lists are equivalent but Ans 12. a A list is mutable, hence it’s value changes after
not identical as they have different objects. function call. However, integer isn’t mutable. Thus its value
Ans 7. d unpack(*x) unpacks the list into the separate doesn’t change.
variables. Now, a=1 and d=4. Thus 5 gets printed. Ans 13. c The above piece of code basically prints the index of
Ans 8. b places1 is an alias of the list places. Hence, any the largest element in the list.
change made to places1 is reflected in places. places2 is a copy of Ans 14. b The syntax isn’t invalid and the list is sorted
the list places. Thus, any change made to places2 isn’t reflected in according to the length of the strings in the list since key is given
places. as len.
Ans 9. a The elements 1 and 2 are first put into separate lists Ans 15. d enumerate(iterator,start=0) is a built-in function
and then combined with a space in between using the join which returns (0,lst[0]),(1,lst[1]) and so on where lst is a
attribute. list(iterator).
Python Questions and Answers – List Comprehension
1. What is the output of the following? if func(i):
k = [print(i) for i in my_string if i not in "aeiou"] list_1.append(expr(i))
a) prints all the vowels in my_string d) none of the mentioned
b) prints all the consonants in my_string 5. What is the output of the following?
c) prints all characters of my_string that aren’t vowels x = [i**+1 for i in range(3)]; print(x);
d) prints only on executing print(k) a) [0, 1, 2]. b) [1, 2, 5].
2. What is the output of print(k) in the following? c) error, **+ is not a valid operator d) error, ‘;’ is not allowed
k = [print(i) for i in my_string if i not in "aeiou"] 6. What is the output of the following?
print(k) print([i.lower() for i in "HELLO"])
a) all characters of my_string that aren’t vowels a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]. b) ‘hello’ c) [‘hello’]. d) hello
b) a list of Nones c) list of Trues d) list of Falses 7. What is the output of the following?
3. What is the output of the following? print([i+j for i in "abc" for j in "def"])
my_string = "hello world" a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’].
k = [(i.upper(), len(i)) for i in my_string]; print(k) b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]].
a) [(‘HELLO’, 5), (‘WORLD’, 5)]. c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]].
b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’].
1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)]. 8. What is the output of the following?
c) [(‘HELLO WORLD’, 11)]. print([[i+j for i in "abc"] for j in "def"])
d) none of the mentioned a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’].
4. Which of the following is the correct expansion of list_1 = b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]].
[expr(i) for i in list_0 if func(i)] ? c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]].
a) list_1 = [] d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’].
for i in list_0: 9. What is the output of the following?
if func(i): print([if i%2==0: i; else: i+1; for i in range(4)])
list_1.append(i) a) [0, 2, 2, 4]. b) [1, 1, 3, 3].
b) for i in list_0: c) error d) none of the mentioned
if func(i): 10. Which of the following is the same as list(map(lambda x:
list_1.append(expr(i)) x**-1, [1, 2, 3]))?
c) list_1 = [] a) [x**-1 for x in [(1, 2, 3)]]. b) [1/x for x in [(1, 2, 3)]].
for i in list_0: c) [1/x for x in (1, 2, 3)]. d) error
SOLUTION
Ans 1. b print() returns None. Ans 6. d If it were to be executed as a nested for loop, i
Ans 2. b We are iterating over each letter in the string. would be the outer loop and j the inner loop.
Ans 3. c We have to create an empty list, loop over the Ans 7. c Syntax error.
contents of the existing list and check if a condition is Ans 8. c x**-1 is evaluated as (x)**(-1).
satisfied before performing some operation and adding it
to the new list.
Ans 4. a i**+1 is evaluated as (i)**(+1).
Ans 5. a We are iterating over each letter in the string.

Page 6 of 6 Computer Science 083 PYTHON [email protected]

You might also like