This set of Python Multiple Choice Questions & Answers (MCQs) 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)] print(m)
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]
View Answer
Explanation: The list comprehension creates a list of lists, where for each x in range(0, 3), it creates [x, x+1, x+2]. This results in [[0, 1, 2], [1, 2, 3], [2, 3, 4]].
2. How many elements are in m?
m = [[x, y] for x in range(0, 4) for y in range(0, 4)] print(m)
a) 8
b) 12
c) 16
d) 32
View Answer
Explanation: The nested list comprehension creates a pair [x, y] for every combination of x and y in range(0, 4), which has 4 values each. So total elements = 4 × 4 = 16.
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
View Answer
Explanation: The code iterates through all elements in the 2D list values, updating v whenever it finds a larger number. The largest value in the nested lists is 33, so the output is 33.
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
View Answer
Explanation: The code goes through all elements in the nested list values and updates v whenever it finds a smaller element (if v > element). Starting with v = 3, it finds smaller values 1 and updates v accordingly. The smallest element in the lists is 1, so the output is 1.
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)
3 4 5 1 1 2 6 33
b)
3 4 5 1 33 6 1 2
c)
1 3 4 5 1 2 6 33
d) 1 3 4 5
View Answer
Explanation: The code sorts each sublist inside the main list, so the first list becomes [1, 3, 4, 5] and the second becomes [1, 2, 6, 33]. It then prints each sorted list on a separate line, resulting in two rows of sorted numbers.
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
View Answer
Explanation: The code prints the element at index 1 (the second element) from each of the 4 rows in matrix. These elements are 2, 5, 9, and 13, which are printed in order separated by spaces.
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
View Answer
Explanation: The function m returns the maximum value in a given list by comparing each element. For the two sublists, it finds 5 as the max in [3, 4, 5, 1] and 33 as the max in [33, 6, 1, 2]. These are printed with a space separating them.
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
View Answer
Explanation: data[1] accesses the second element of the outer list: [[5, 6], [7, 8]]. Then [0] accesses the first sublist [5, 6], and [0] again accesses the first element of that sublist, which is 5.
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
View Answer
Explanation: The function ttt finds the maximum element in the 2D list data[0], which is [[1, 2], [3, 4]]. The largest number here is 4, so the output is 4.
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]]
View Answer
Explanation: The sort() method sorts lists of lists by comparing their elements lexicographically, starting from the first element of each sublist. Here, the points are sorted by their first coordinate, resulting in the order [[0.5, 0.5], [1, 2], [3, 1.5]].
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Python Internship
- Apply for Programming Internship
- Check Information Technology Books
- Check Python Books
- Practice Programming MCQs