Python Questions and Answers - Lists - 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Python Questions and Answers – Lists – 1

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Lists”.
1. Which of the following commands will create a list?
a) list1 = list()
b) list1 = []
c) list1 = list([1, 2, 3])
d) all of the mentioned

Answer: d
Explanation: Execute in the shell to verify
2. What is the output when we execute list(“hello”)?
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) [‘hello’]
c) [‘llo’]
d) [‘olleh’]

Answer: a
Explanation: Execute in the shell to verify.
3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a) 5
b) 4
c) None
d) Error

Answer: a
Explanation: Execute in the shell and verify.
advertisement
4. Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445
b) 133
c) 12454
d) 123

Answer: c
Explanation: Max returns the maximum element in the list.
5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?
a) 3
b) 5
c) 25
d) 1

Answer: d
Explanation: Min returns the minimum element in the list.
6. Suppose list1 is [1, 5, 9], what is sum(list1)?
a) 1
b) 9
c) 15
d) Error
Answer: c
Explanation: Sum returns the sum of all elements in the list.
7. To shuffle the list(say list1) what function do we use?
a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)
d) random.shuffleList(list1)

Answer: c
Explanation: Execute in the shell to verify.
8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing operation?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned

Answer: d
Explanation: Slicing is allowed in lists just as in the case of strings.
9. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
a) Error
b) None
c) 25
d) 2

Answer: c
Explanation: -1 corresponds to the last index in the list.
10. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]

Answer: a
Explanation: Execute in the shell to verify.

Python Questions and Answers – Lists – 2

This set of Python Coding Interview Questions & Answers focuses on “Lists”.
1. What will be the output of the following Python code?
>>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
>>>print(names[-1][-1])
a) A
b) Daman
c) Error
d) n

Answer: d
Explanation: Execute in the shell to verify.
advertisement
2. What will be the output of the following Python code?
names1 = ['Amir', 'Bear', 'Charlton', 'Daman']
names2 = names1
names3 = names1[:]
 
names2[0] = 'Alice'
names3[1] = 'Bob'
 
sum = 0
for ls in (names1, names2, names3):
if ls[0] == 'Alice':
sum += 1
if ls[1] == 'Bob':
sum += 10
 
print sum
a) 11
b) 12
c) 21
d) 22

Answer: b
Explanation: When assigning names1 to names2, we create a second reference to the same list.
Changes to names2 affect names1. When assigning the slice of all elements in names1 to names3,
we are creating a full copy of names1 which can be modified independently.
3. Suppose list1 is [1, 3, 2], What is list1 * 2?
a) [2, 6, 4]
b) [1, 3, 2, 1, 3]
c) [1, 3, 2, 1, 3, 2]
d) [1, 3, 2, 3, 2, 1]

Answer: c
Explanation: Execute in the shell and verify.
4. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:
a) [0, 1, 2, 3]
b) [0, 1, 2, 3, 4]
c) [0.0, 0.5, 1.0, 1.5]
d) [0.0, 0.5, 1.0, 1.5, 2.0]

Answer: c
Explanation: Execute in the shell to verify.
5. What will be the output of the following Python code?
>>>list1 = [11, 2, 23]
>>>list2 = [11, 2, 2]
>>>list1 < list2 is
a) True
b) False
c) Error
d) None
Answer: b
Explanation: Elements are compared one by one.
6. To add a new element to a list we use which command?
a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5)

Answer: b
Explanation: We use the function append to add an element to the list.
7. To insert 5 to the third position in list1, we use which command?
a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)

Answer: b
Explanation: Execute in the shell to verify.
8. To remove string “hello” from list1, we use which command?
a) list1.remove(“hello”)
b) list1.remove(hello)
c) list1.removeAll(“hello”)
d) list1.removeOne(“hello”)

Answer: a
Explanation: Execute in the shell to verify.
9. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?
a) 0
b) 1
c) 4
d) 2

Answer: d
Explanation: Execute help(list.index) to get details.
10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?
a) 0
b) 4
c) 1
d) 2

Answer: d
Explanation: Execute in the shell to verify.

Python Questions and Answers – Lists – 3

This set of Python Programming Questions & Answers focuses on “Lists”.


1. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [25, 20, 5, 5, 4, 3, 3, 1]
d) [3, 1, 25, 5, 20, 5, 4, 3]

Answer: d
Explanation: Execute in the shell to verify.
2. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?
a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]
b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]
c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]
d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]

Answer: a
Explanation: Execute in the shell to verify.
3. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?
a) [3, 4, 5, 20, 5, 25, 1, 3]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]

Answer: c
Explanation: pop() removes the element at the position specified in the parameter.
advertisement
4. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]

Answer: a
Explanation: pop() by default will remove the last element.
5. What will be the output of the following Python code?
>>>"Welcome to Python".split()
a) [“Welcome”, “to”, “Python”]
b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”

Answer: a
Explanation: split() function returns the elements in a list.
6. What will be the output of the following Python code?
>>>list("a#b#c#d".split('#'))
a) [‘a’, ‘b’, ‘c’, ‘d’]
b) [‘a b c d’]
c) [‘a#b#c#d’]
d) [‘abcd’]

Answer: a
Explanation: Execute in the shell to verify.
7. What will be the output of the following Python code?
myList = [1, 5, 5, 5, 5, 1]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
 
>>>print(indexOfMax)
a) 1
b) 2
c) 3
d) 4

Answer: a
Explanation: First time the highest number is encountered is at index 1.
8. What will be the output of the following Python code?
myList = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
myList[i - 1] = myList[i]
 
for i in range(0, 6):
print(myList[i], end = " ")
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

Answer: c
Explanation: Execute in the shell to verify.
9. What will be the output of the following Python code?
>>>list1 = [1, 3]
>>>list2 = list1
>>>list1[0] = 4
>>>print(list2)
a) [1, 3]
b) [4, 3]
c) [1, 4]
d) [1, 3, 4]

Answer: b
Explanation: Lists should be copied by executing [:] operation.
10. What will be the output of the following Python code?
def f(values):
values[0] = 44
 
v = [1, 2, 3]
f(v)
print(v)
a) [1, 44]
b) [1, 2, 3, 44]
c) [44, 2, 3]
d) [1, 2, 3]
Answer: c
Explanation: Execute in the shell to verify.

Python Questions and Answers – Lists – 4


This set of Python Programming Interview Questions & Answers focuses on “Lists”.
1. What will be the output of the following Python code?
def f(i, values = []):
values.append(i)
return values
 
f(1)
f(2)
v = f(3)
print(v)
a) [1] [2] [3]
b) [1] [1, 2] [1, 2, 3]
c) [1, 2, 3]
d) 1 2 3

Answer: c
Explanation: Execute in the shell to verify
advertisement
2. What will be the output of the following Python code?
names1 = ['Amir', 'Bala', 'Chales']
 
if 'amir' in names1:
print(1)
else:
print(2)
a) None
b) 1
c) 2
d) Error

Answer: c
Explanation: Execute in the shell to verify.
3. What will be the output of the following Python code?
names1 = ['Amir', 'Bala', 'Charlie']
names2 = [name.lower() for name in names1]
 
print(names2[2][0])
a) None
b) a
c) b
d) c

Answer: d
Explanation: List Comprehension are a shorthand for creating new lists.
4. What will be the output of the following Python code?
numbers = [1, 2, 3, 4]
 
numbers.append([5,6,7,8])
 
print(len(numbers))
a) 4
b) 5
c) 8
d) 12

Answer: b
Explanation: A list is passed in append so the length is 5.
5. To which of the following the “in” operator can be used to check if an item is in it?
a) Lists
b) Dictionary
c) Set
d) All of the mentioned

Answer: d
Explanation: In can be used in all data structures.
6. What will be the output of the following Python code?
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
 
print(len(list1 + list2))
a) 2
b) 4
c) 5
d) 8

Answer: d
Explanation: + appends all the elements individually into a new list.
7. What will be the output of the following Python code?
def addItem(listParam):
listParam += [1]
 
mylist = [1, 2, 3, 4]
addItem(mylist)
print(len(mylist))
a) 1
b) 4
c) 5
d) 8

Answer: c
Explanation: + will append the element to the list.
8. What will be the output of the following Python code?
def increment_items(L, increment):
i=0
while i < len(L):
L[i] = L[i] + increment
i=i+1
 
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)
a)
None
[3, 4, 5]
b)
None
[1, 2, 3]
c)
[3, 4, 5]
[1, 2, 3]
d)
[3, 4, 5]
None

Answer: a
Explanation: Execute in the shell to verify.
 
 
9. What will be the output of the following Python code?
def example(L):
''' (list) -> list
'''
i=0
result = []
while i < len(L):
result.append(L[i])
i=i+3
return result
a) Return a list containing every third item from L starting at index 0
b) Return an empty list
c) Return a list containing every third index from L starting at index 0
d) Return a list containing the items from L starting from index 0, omitting every third item

Answer: a
Explanation: Run the code to get a better understanding with many arguments.
10. What will be the output of the following Python code?
veggies = ['carrot', 'broccoli', 'potato', 'asparagus']
veggies.insert(veggies.index('broccoli'), 'celery')
print(veggies)
a) [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’] Correct 1.00
b) [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]

c) [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]

d) [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]

Answer: a
Explanation: Execute in the shell to verify.
Python Questions and Answers – Lists – 5

This set of Python Question Paper focuses on “Lists”.


1. What will be the output of the following Python code?
>>>m = [[x, x + 1, x + 2] for x in range(0, 3)]
a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
c) [1, 2, 3, 4, 5, 6, 7, 8, 9]
d) [0, 1, 2, 1, 2, 3, 2, 3, 4]

Answer: b
Explanation: Execute in the shell to verify.
advertisement
2. How many elements are in m?
m = [[x, y] for x in range(0, 4) for y in range(0, 4)]
a) 8
b) 12
c) 16
d) 32

Answer: c
Explanation: Execute in the shell to verify.
3. What will be the output of the following Python code?
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
 
v = values[0][0]
for row in range(0, len(values)):
for column in range(0, len(values[row])):
if v < values[row][column]:
v = values[row][column]
 
print(v)
a) 3
b) 5
c) 6
d) 33

Answer: d
Explanation: Execute in the shell to verify.
4. What will be the output of the following Python code?
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
 
v = values[0][0]
for lst in values:
for element in lst:
if v > element:
v = element
 
print(v)
a) 1
b) 3
c) 5
d) 6

Answer: a
Explanation: Execute in the shell to verify.
5. What will be the output of the following Python code?
values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
 
for row in values:
row.sort()
for element in row:
print(element, end = " ")
print()
a) The program prints two rows 3 4 5 1 followed by 33 6 1 2
b) The program prints on row 3 4 5 1 33 6 1 2
c) The program prints two rows 3 4 5 1 followed by 33 6 1 2
d) The program prints two rows 1 3 4 5 followed by 1 2 6 33

Answer: d
Explanation: Execute in the shell to verify.
6. What will be the output of the following Python code?
matrix = [[1, 2, 3, 4],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15]]
 
for i in range(0, 4):
print(matrix[i][1], end = " ")
a) 1 2 3 4
b) 4 5 6 7
c) 1 3 8 12
d) 2 5 9 13

Answer: d
Explanation: Execute in the shell to verify.
7. What will be the output of the following Python code?
def m(list):
v = list[0]
for e in list:
if v < e: v = e
return v
 
values = [[3, 4, 5, 1], [33, 6, 1, 2]]
 
for row in values:
print(m(row), end = " ")
a) 3 33
b) 1 1
c) 5 6
d) 5 33

Answer: d
Explanation: Execute in the shell to verify.
8. What will be the output of the following Python code?
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
 
print(data[1][0][0])
a) 1
b) 2
c) 4
d) 5

Answer: d
Explanation: Execute in the shell to verify.
9. What will be the output of the following Python code?
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
 
def ttt(m):
v = m[0][0]
 
for row in m:
for element in row:
if v < element: v = element
 
return v
 
print(ttt(data[0]))
a) 1
b) 2
c) 4
d) 5

Answer: c
Explanation: Execute in the shell to verify.
10. What will be the output of the following Python code?
points = [[1, 2], [3, 1.5], [0.5, 0.5]]
points.sort()
print(points)
a) [[1, 2], [3, 1.5], [0.5, 0.5]]
b) [[3, 1.5], [1, 2], [0.5, 0.5]]
c) [[0.5, 0.5], [1, 2], [3, 1.5]]
d) [[0.5, 0.5], [3, 1.5], [1, 2]]

Answer: c
Explanation: Execute in the shell to verify.

Python Questions and Answers – Lists – 6


This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Lists-6”.
1. What will be the output of the following Python code?
a=[10,23,56,[78]]
b=list(a)
a[3][0]=95
a[1]=34
print(b)
a) [10,34,56,[95]]
b) [10,23,56,[78]]
c) [10,23,56,[95]]
d) [10,34,56,[78]]

Answer: c
Explanation: The above copy is a type of shallow copy and only changes made in sublist is reflected
in the copied list.
advertisement
2. What will be the output of the following Python code?
print(list(zip((1,2,3),('a'),('xxx','yyy'))))
print(list(zip((2,4),('b','c'),('yy','xx'))))
a)
[(1,2,3),(‘a’),(‘xxx’,’yyy’)]
[(2,4),(‘b’,’c’),(‘yy’,’xx’)]
b)
[(1, 'a', 'xxx'),(2,’ ‘,’yyy’),(3,’ ‘,’ ‘)]
[(2, 'b', 'yy'), (4, 'c', 'xx')]
c) Syntax error
d)
[(1, 'a', 'xxx')]
[(2, 'b', 'yy'), (4, 'c', 'xx')]

Answer: d
Explanation: The zip function combines the individual attributes of the lists into a list of tuples.
 
 
3. What will be the output of the following Python code?
import copy
a=[10,23,56,[78]]
b=copy.deepcopy(a)
a[3][0]=95
a[1]=34
print(b)
a) [10,34,56,[95]]
b) [10,23,56,[78]]
c) [10,23,56,[95]]
d) [10,34,56,[78]]

Answer: b
Explanation: The above copy is deepcopy. Any change made in the original list isn’t reflected.
4. What will be the output of the following Python code?
s="a@b@c@d"
a=list(s.partition("@"))
print(a)
b=list(s.split("@",3))
print(b)
a)
[‘a’,’b’,’c’,’d’]
[‘a’,’b’,’c’,’d’]
b)
[‘a’,’@’,’b’,’@’,’c’,’@’,’d’]
[‘a’,’b’,’c’,’d’]
c)
[‘a’,’@’,’b@c@d’]
[‘a’,’b’,’c’,’d’]
d)
[‘a’,’@’,’b@c@d’]
[‘a’,’@’,’b’,’@’,’c’,’@’,’d’]

Answer: c
Explanation: 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.
 
 
5. What will be the output of the following Python code?
a=[1,2,3,4]
b=[sum(a[0:x+1]) for x in range(0,len(a))]
print(b)
a) 10
b) [1,3,5,7]
c) 4
d) [1,3,6,10]

Answer: d
Explanation: The above code returns the cumulative sum of elements in a list.
6. What will be the output of the following Python code?
a="hello"
b=list((x.upper(),len(x)) for x in a)
print(b)
a) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1)]
b) [(‘HELLO’, 5)]
c) [(‘H’, 5), (‘E’, 5), (‘L’, 5), (‘L’, 5), (‘O’, 5)]
d) Syntax error

Answer: a
Explanation: Variable x iterates over each letter in string a hence the length of each letter is 1.
7. What will be the output of the following Python code?
a=[1,2,3,4]
b=[sum(a[0:x+1]) for x in range(0,len(a))]
print(b)
a) 10
b) [1,3,5,7]
c) 4
d) [1,3,6,10]

Answer: d
Explanation: The above code returns the cumulative sum of elements in a list.
8. What will be the output of the following Python code?
a=[[]]*3
a[1].append(7)
print(a)
a) Syntax error
b) [[7], [7], [7]]
c) [[7], [], []]
d) [[],7, [], []]

Answer: b
Explanation: The first line of the code creates multiple reference copies of sublist. Hence when 7 is
appended, it gets appended to all the sublists.
9. What will be the output of the following Python code?
b=[2,3,4,5]
a=list(filter(lambda x:x%2,b))
print(a)
a) [2,4]
b) [ ]
c) [3,5]
d) Invalid arguments for filter function

Answer: c
Explanation: The filter function gives value from the list b for which the condition is true, that is, x
%2==1.
10. What will be the output of the following Python code?
lst=[3,4,6,1,2]
lst[1:2]=[7,8]
print(lst)
a) [3, 7, 8, 6, 1, 2]
b) Syntax error
c) [3,[7,8],6,1,2]
d) [3,4,6,7,8]

Answer: a
Explanation: 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 – Strings – 7


This set of Online Python Quiz focuses on “Strings”.
1. What will be the output of the following Python code?
print("ab\tcd\tef".expandtabs())
a) ab  cd  ef
b) abcdef
c) ab\tcd\tef
d) ab cd ef

Answer: a
Explanation: Each \t is converted to 8 blank spaces by default.
advertisement
2. What will be the output of the following Python code?
print("ab\tcd\tef".expandtabs(4))
a) ab   cd   ef
b) abcdef
c) ab\tcd\tef
d) ab cd ef

Answer: d
Explanation: Each \t is converted to 4 blank spaces.
3. What will be the output of the following Python code?
print("ab\tcd\tef".expandtabs('+'))
a) ab+cd+ef
b) ab++++++++cd++++++++ef
c) ab cd ef
d) none of the mentioned

Answer: d
Explanation: TypeError, an integer should be passed as an argument.
4. What will be the output of the following Python code?
print("abcdef".find("cd") == "cd" in "abcdef")
a) True
b) False
c) Error
d) None of the mentioned

Answer: b
Explanation: The function find() returns the position of the sunstring in the given string whereas the
in keyword returns a value of Boolean type.
5. What will be the output of the following Python code?
print("abcdef".find("cd"))
a) True
b) 2
c) 3
d) None of the mentioned

Answer: b
Explanation: The first position in the given string at which the substring can be found is returned.
6. What will be the output of the following Python code?
print("ccdcddcd".find("c"))
a) 4
b) 0
c) Error
d) True

Answer: b
Explanation: The first position in the given string at which the substring can be found is returned.
7. What will be the output of the following Python code?
print("Hello {0} and {1}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {0} and {1} foo bin
c) Error
d) Hello 0 and 1

Answer: a
Explanation: The numbers 0 and 1 represent the position at which the strings are present.
8. What will be the output of the following Python code?
print("Hello {1} and {0}".format('bin', 'foo'))
a) Hello foo and bin
b) Hello bin and foo
c) Error
d) None of the mentioned

Answer: a
Explanation: The numbers 0 and 1 represent the position at which the strings are present.
9. What will be the output of the following Python code?
print("Hello {} and {}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {} and {}
c) Error
d) Hello and

Answer: a
Explanation: It is the same as Hello {0} and {1}.

10. What will be the output of the following Python code?


print("Hello {name1} and {name2}".format('foo', 'bin'))
a) Hello foo and bin
b) Hello {name1} and {name2}
c) Error
d) Hello and

Answer: c
Explanation: The arguments passed to the function format aren’t keyword arguments

Python Questions and Answers – List Comprehension


This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “List
Comprehension”.
1. What will be the output of the following Python code snippet?
k = [print(i) for i in my_string if i not in "aeiou"]
a) prints all the vowels in my_string
b) prints all the consonants in my_string
c) prints all characters of my_string that aren’t vowels
d) prints only on executing print(k)
Answer: c
Explanation: print(i) is executed if the given character is not a vowel.
advertisement
2. What is the output of print(k) in the following Python code snippet?
k = [print(i) for i in my_string if i not in "aeiou"]
print(k)
a) all characters of my_string that aren’t vowels
b) a list of Nones
c) list of Trues
d) list of Falses

Answer: b
Explanation: print() returns None.
3. What will be the output of the following Python code snippet?
my_string = "hello world"
k = [(i.upper(), len(i)) for i in my_string]
print(k)
a) [(‘HELLO’, 5), (‘WORLD’, 5)]
b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)]
c) [(‘HELLO WORLD’, 11)]
d) none of the mentioned

Answer: b
Explanation: We are iterating over each letter in the string.
4. Which of the following is the correct expansion of list_1 = [expr(i) for i in list_0 if
func(i)]?
a)
list_1 = []
for i in list_0:
if func(i):
list_1.append(i)
b)
for i in list_0:
if func(i):
list_1.append(expr(i))
c)
list_1 = []
for i in list_0:
if func(i):
list_1.append(expr(i))
d) none of the mentioned

Answer: c
Explanation: We have to create an empty list, loop over the contents of the existing list
and check if a condition is satisfied before performing some operation and adding it to
the new list.
5. What will be the output of the following Python code snippet?
x = [i**+1 for i in range(3)]; print(x);
a) [0, 1, 2]
b) [1, 2, 5]
c) error, **+ is not a valid operator
d) error, ‘;’ is not allowed

Answer: a
Explanation: i**+1 is evaluated as (i)**(+1).
6. What will be the output of the following Python code snippet?
print([i.lower() for i in "HELLO"])
a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) ‘hello’
c) [‘hello’]
d) hello

Answer: a
Explanation: We are iterating over each letter in the string.
7. What will be the output of the following Python code snippet?
print([i+j for i in "abc" for j in "def"])
a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]

Answer: d
Explanation: If it were to be executed as a nested for loop, i would be the outer loop and
j the inner loop.
8. What will be the output of the following Python code snippet?
print([[i+j for i in "abc"] for j in "def"])
a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]

Answer: b
Explanation: The inner list is generated once for each value of j.
9. What will be the output of the following Python code snippet?
print([if i%2==0: i; else: i+1; for i in range(4)])
a) [0, 2, 2, 4]
b) [1, 1, 3, 3]
c) error
d) none of the mentioned

Answer: c
Explanation: Syntax error.
10. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
a) [x**-1 for x in [(1, 2, 3)]]
b) [1/x for x in [(1, 2, 3)]]
c) [1/x for x in (1, 2, 3)]
d) error

Answer: c
Explanation: x**-1 is evaluated as (x)**(-1).
Python Questions and Answers – List Comprehension – 1
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “List
Comprehension – 1”.
1. What will be the output of the following Python code?
l=[1,2,3,4,5]
[x&1 for x in l]
a) [1, 1, 1, 1, 1]
b) [1, 0, 1, 0, 1]
c) [1, 0, 0, 0, 0]
d) [0, 1, 0, 1, 0]

Answer: b
Explanation: In the code shown above, each of the numbers of the list, that is, 1, 2, 3, 4
and 5 are AND-ed with 1 and the result is printed in the form of a list. Hence the output is
[1, 0, 1, 0, 1].
advertisement
2. What will be the output of the following Python code?
l1=[1,2,3]
l2=[4,5,6]
[x*y for x in l1 for y in l2]
a) [4, 8, 12, 5, 10, 15, 6, 12, 18]
b) [4, 10, 18]
c) [4, 5, 6, 8, 10, 12, 12, 15, 18]
d) [18, 12, 6, 15, 10, 5, 12, 8, 4]

Answer: c
Explanation: The code shown above returns x*y, where x belongs to the list l1 and y
belongs to the list l2. Therefore, the output is: [4, 5, 6, 8, 10, 12, 12, 15, 18].
3. Write the list comprehension to pick out only negative integers from a given list ‘l’.
a) [x<0 in l]
b) [x for x<0 in l]
c) [x in l for x<0]
d) [x for x in l if x<0]

Answer: d
Explanation: To pick out only the negative numbers from a given list ‘l’, the correct list
comprehension statement would be: [x for x in l if x<0].

For example if we have a list l=[-65, 2, 7, -99, -4, 3]


>>> [x for x in l if x<0]
The output would be: [-65, -99, -4].
4. What will be the output of the following Python code?
s=["pune", "mumbai", "delhi"]
[(w.upper(), len(w)) for w in s]
a) Error
b) [‘PUNE’, 4, ‘MUMBAI’, 6, ‘DELHI’, 5]
c) [PUNE, 4, MUMBAI, 6, DELHI, 5]
d) [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)]
Answer: d
Explanation: If we need to generate two results, we need to put it in the form of a tuple.
The code shown above returns each word of list in uppercase, along with the length of
the word. Hence the output of the code is: [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)].
5. What will be the output of the following Python code?
l1=[2,4,6]
l2=[-2,-4,-6]
for i in zip(l1, l2):
print(i)
a)
2, -2
4, -4
6, -6
b) [(2, -2), (4, -4), (6, -6)]
c)
(2, -2)
(4, -4)
(6, -6)
d) [-4, -16, -36]

Answer: c
Explanation: The output of the code shown will be:
(2, -2)
(4, -4)
(6, -6)
This format is due to the statement print(i).
6. What will be the output of the following Python code?
l1=[10, 20, 30]
l2=[-10, -20, -30]
l3=[x+y for x, y in zip(l1, l2)]
l3
a) Error
b) 0
c) [-20, -60, -80]
d) [0, 0, 0]

Answer: d
Explanation: The code shown above returns x+y, for x belonging to the list l1 and y
belonging to the list l2. That is, l3=[10-10, 20-20, 30-20], which is, [0, 0, 0].
7. Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9].
a) [x**3 for x in l]
b) [x^3 for x in l]
c) [x**3 in l]
d) [x^3 in l]

Answer: a
Explanation: The list comprehension to print a list of cube of the numbers for the given
list is: [x**3 for x in l].
8. What will be the output of the following Python code?
l=[[1 ,2, 3], [4, 5, 6], [7, 8, 9]]
[[row[i] for row in l] for i in range(3)]
a) Error
b) [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
c)
147
258
369
d)
(1 4 7)
(2 5 8)
(3 6 9)

Answer: b
Explanation: In the code shown above, ‘3’ is the index of the list. Had we used a number
greater than 3, it would result in an error. The output of this code is: [[1, 4, 7], [2, 5, 8], [3,
6, 9]].
 
 
9. What will be the output of the following Python code?
import math
[str(round(math.pi)) for i in range (1, 6)]
a) [‘3’, ‘3’, ‘3’, ‘3’, ‘3’, ‘3’]
b) [‘3.1’, ‘3.14’, ‘3.142’, ‘3.1416’, ‘3.14159’, ‘3.141582’]
c) [‘3’, ‘3’, ‘3’, ‘3’, ‘3’]
d) [‘3.1’, ‘3.14’, ‘3.142’, ‘3.1416’, ‘3.14159’]

Answer: c
Explanation: The list comprehension shown above rounds off pi(3.141) and returns its
value, that is 3. This is done 5 times. Hence the output is: [‘3’, ‘3’, ‘3’, ‘3’, ‘3’].
10. What will be the output of the following Python code?
l1=[1,2,3]
l2=[4,5,6]
l3=[7,8,9]
for x, y, z in zip(l1, l2, l3):
print(x, y, z)
a)
147
258
369
b)
(1 4 7)
(2 5 8)
(3 6 9)
c) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
d) Error

Answer: a
Explanation: The output of the code shown above is:
147
258
369
This is due to the statement: print(x, y,z).
Python Questions and Answers – List Comprehension – 2
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “List
Comprehension – 2”.
1. Read the information given below carefully and write a list comprehension such that
the output is: [‘e’, ‘o’]
w="hello"
v=('a', 'e', 'i', 'o', 'u')
a) [x for w in v if x in v]
b) [x for x in w if x in v]
c) [x for x in v if w in v]
d) [x for v in w for x in w]

Answer: b
Explanation: The tuple ‘v’ is used to generate a list containing only vowels in the string
‘w’. The result is a list containing only vowels present in the string “hello”. Hence the
required list comprehension is: [x for x in w if x in v].
advertisement
2. What will be the output of the following Python code?
[ord(ch) for ch in 'abc']
a) [97, 98, 99]
b) [‘97’, ‘98’, ‘99’]
c) [65, 66, 67]
d) Error

Answer: a
Explanation: The list comprehension shown above returns the ASCII value of each
alphabet of the string ‘abc’. Hence the output is: [97, 98, 99]. Had the string been ‘ABC’,
the output would be: [65, 66, 67].
3. What will be the output of the following Python code?
t=32.00
[round((x-32)*5/9) for x in t]
a) [0]
b) 0
c) [0.00]
d) Error

Answer: d
Explanation: The value of t in the code shown above is equal to 32.00, which is a floating
point value. ‘Float’ objects are not iterable. Hence the code results in an error.
4. Write a list comprehension for producing a list of numbers between 1 and 1000 that
are divisible by 3.
a) [x in range(1, 1000) if x%3==0]
b) [x for x in range(1000) if x%3==0]
c) [x%3 for x in range(1, 1000)]
d) [x%3=0 for x in range(1, 1000)]

Answer: b
Explanation: The list comprehension [x for x in range(1000) if x%3==0] produces a list of
numbers between 1 and 1000 that are divisible by 3.
5. Write a list comprehension equivalent for the Python code shown below.
for i in range(1, 101):
if int(i*0.5)==i*0.5:
print(i)
a) [i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
b) [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
c) [i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
d) [i for i in range(1, 100) if int(i*0.5)=(i*0.5)]

Answer: b
Explanation: The code shown above prints the value ‘i’ only if it satisfies the condition:
int(i*0.5) is equal to (i*0.5). Hence the required list comprehension is: [i for i in range(1,
101) if int(i*0.5)==(i*0.5)].
6. What is the list comprehension equivalent for: list(map(lambda x:x**-1, [1, 2, 3]))?
a) [1|x for x in [1, 2, 3]]
b) [-1**x for x in [1, 2, 3]]
c) [x**-1 for x in [1, 2, 3]]
d) [x^-1 for x in range(4)]

Answer: c
Explanation: The output of the function list(map(lambda x:x**-1, [1, 2, 3])) is [1.0, 0.5,
0.3333333333333333] and that of the list comprehension [x**-1 for x in [1, 2, 3]] is [1.0,
0.5, 0.3333333333333333]. Hence the answer is: [x**-1 for x in [1, 2, 3]].
7. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
a) [(2**x) for x in range(0, 13)]
b) [(x**2) for x in range(1, 13)]
c) [(2**x) for x in range(1, 13)]
d) [(x**2) for x in range(0, 13)]

Answer: a
Explanation: The required list comprehension will print the numbers from 1 to 12, each
raised to 2. The required answer is thus, [(2**x) for x in range(0, 13)].
8. What is the list comprehension equivalent for?
{x : x is a whole number less than 20, x is even} (including zero)
a) [x for x in range(1, 20) if (x%2==0)]
b) [x for x in range(0, 20) if (x//2==0)]
c) [x for x in range(1, 20) if (x//2==0)]
d) [x for x in range(0, 20) if (x%2==0)]

Answer: d
Explanation: The required list comprehension will print a whole number, less than 20,
provided that the number is even. Since the output list should contain zero as well, the
answer to this question is: [x for x in range(0, 20) if (x%2==0)].
9. What will be the output of the following Python list comprehension?
[j for i in range(2,8) for j in range(i*2, 50, i)]
a) A list of prime numbers up to 50
b) A list of numbers divisible by 2, up to 50
c) A list of non prime numbers, up to 50
d) Error

Answer: c
Explanation: The list comprehension shown above returns a list of non-prime numbers
up to 50. The logic behind this is that the square root of 50 is almost equal to 7. Hence
all the multiples of 2-7 are not prime in this range.
10. What will be the output of the following Python code?
l=["good", "oh!", "excellent!", "#450"]
[n for n in l if n.isalpha() or n.isdigit()]
a) [‘good’, ‘oh’, ‘excellent’, ‘450’ ]
b) [‘good’]
c) [‘good’, ‘#450’]
d) [‘oh!’, ‘excellent!’, ‘#450’]

Answer: b
Explanation: The code shown above returns a new list containing only strings which do
not have any punctuation in them. The only string from the list which does not contain
any punctuation is ‘good’. Hence the output of the code shown above is [‘good’].

Python Questions and Answers – Matrix List Comprehension


This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Matrix List
Comprehension”.
1. Which of the following matrices will throw an error in Python?
a)
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
b)
B = [[3, 3, 3]
[4, 4, 4]
[5, 5, 5]]
c)
C = [(1, 2, 4),
(5, 6, 7),
(8, 9, 10)]
advertisement
d)
D = [2, 3, 4,
3, 3, 3,
4, 5, 6]

Answer: b
Explanation: In matrix B will result in an error because in the absence of a comma at the
end of each row, it behaves like three separate lists. The error thrown states that the list
integers must be integers or slices, not tuples.
 
 
2. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
A[1]
a) [4, 5, 6]
b) [3, 6, 9]
c) [1, 4, 7]
d) [1, 2, 3]
Answer: a
Explanation: We can index the rows and columns using normal index operations. The
statement A[1] represents the second row, that is, the middle row. Hence the output of
the code will be: [4, 5, 6].
3. Which of the following Python statements will result in the output: 6?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
a) A[2][3]
b) A[2][1]
c) A[1][2]
d) A[3][2]

Answer: c
Explanation: The output that is required is 6, that is, row 2, item 3. This position is
represented by the statement: A[1][2].
4. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[A[row][1] for row in (0, 1, 2)]
a) [7, 8, 9]
b) [4, 5, 6]
c) [2, 5, 8]
d) [1, 4, 7]

Answer: c
Explanation: To get a particular column as output, we can simple iterate across the rows
and pull out the desired column, or iterate through positions in rows and index as we go.
Hence the output of the code shown above is: [2, 5, 8].
5. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[A[i][i] for i in range(len(A))]
a) [1, 5, 9]
b) [3, 5, 7]
c) [4, 5, 6]
d) [2, 5, 8]

Answer: a
Explanation: We can also perform tasks like pulling out a diagonal. The expression
shown above uses range to generate the list of offsets and the indices with the row and
column the same, picking out A[0][0], then A[1][1] and so on. Hence the output of the
code is: [1, 5, 9].
6. What will be the output of the following Python code?
l=[[1, 2, 3], [4, 5, 6]]
for i in range(len(l)):
for j in range(len(l[i])):
l[i][j]+=10
l
a) No output
b) Error
c) [[1, 2, 3], [4, 5, 6]]
d) [[11, 12, 13], [14, 15, 16]]

Answer: d
Explanation: We use range twice if the shapes differ. Each element of list l is increased
by 10. Hence the output is: [[11, 12, 13], [14, 15, 16]]
7. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
 
[[col + 10 for col in row] for row in A]
a) [[11, 12, 13], [14, 15, 16], [17, 18, 19]]
b) Error
c) [11, 12, 13], [14, 15, 16], [17, 18, 19]
d) [11, 12, 13, 14, 15, 16, 17, 18, 19]

Answer: a
Explanation: The code shown above shows a list comprehension which adds 10 to each
element of the matrix A and prints it row-wise. Hence the output of the code is: [[11, 12,
13], [14, 15, 16], [17, 18, 19]]
8. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[A[i][len(A)-1-i] for i in range(len(A))]
a) [1, 5, 9]
b) [4, 5, 6]
c) [3, 5, 7]
d) [2, 5, 8]

Answer: c
Explanation: This expression scales the common index to fetch A[0][2], A[1][1], etc. We
assume the matrix has the same number of rows and columns.
9. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]
[B[row][col]*A[row][col] for row in range(3) for col in range(3)]
a) [3, 6, 9, 16, 20, 24, 35, 40, 45]
b) Error
c) [0, 30, 60, 120, 160, 200, 300, 350, 400]
d) 0

Answer: a
Explanation: In the code shown above, we have used list comprehension to combine
values of multiple matrices. We have multiplied the elements of the matrix B with that of
the matrix A, in the range(3). Hence the output of this code is: [3, 6, 9, 16, 20, 24, 35, 40,
45].
10. What will be the output of the following Python code?
r = [11, 12, 13, 14, 15, 16, 17, 18, 19]
A = [[0, 10, 20],
[30, 40, 50],
[60, 70, 80]]
for row in A:
for col in row:
r.append(col+10)
r
a) [11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 20, 30, 40, 50, 60, 70, 80, 90]
b) [10, 20, 30, 40, 50, 60, 70, 80, 90]
c) [11, 12, 13, 14, 15, 16, 17, 18, 19]
d) [0, 10, 20, 30, 40, 50, 60, 70, 80]

Answer: a
Explanation: The code shown above adds 10 to each element of the matrix and prints
the output row-wise. Since the list l already contains some elements, the new elements
are appended to it. Hence the output of this code is: [11, 12, 13, 14, 15, 16, 17, 18, 19,
10, 20, 30, 40, 50, 60, 70, 80, 90].
11. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]
[[col1 * col2 for (col1, col2) in zip(row1, row2)] for (row1, row2) in zip(A, B)]
a) [0, 30, 60, 120, 160, 200, 300, 350, 400]
b) [[3, 6, 9], [16, 20, 24], [35, 40, 45]]
c) No output
d) Error

Answer: b
Explanation: The list comprehension shown above results in the output: [[3, 6, 9], [16,
20, 24], [35, 40, 45]].
12. What will be the output of the following Python code?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]
zip(A, B)
a) Address of the zip object
b) Address of the matrices A and B
c) No output
d) [3, 6, 9, 16, 20, 24, 35, 40, 45]

Answer: a
Explanation: The output of the code shown above returns the address of the zip object. If
we print it in the form of a list, we get:
>>> list(zip(A, B))
[([1, 2, 3], [3, 3, 3]), ([4, 5, 6], [4, 4, 4]), ([7, 8, 9], [5, 5, 5])]

You might also like