Python Questions and Answers – Lists – 5
sanfoundry.com/python-question-papers
August 6, 2017
1. What will be the output of the following Python code?
1. >>>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]
View Answer
Answer: b
Explanation: Execute in the shell to verify.
2. How many elements are in m?
1. m = [[x, y] for x in range(0, 4) for y in range(0, 4)]
a) 8
b) 12
c) 16
d) 32
View Answer
Answer: c
Explanation: Execute in the shell to verify.
Note: Join free Sanfoundry classes at Telegram or Youtube
3. What will be the output of the following Python code?
1. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
2. v = values[0][0]
3. for row in range(0, len(values)):
4. for column in range(0, len(values[row])):
5. if v < values[row][column]:
6. v = values[row][column]
7. print(v)
a) 3
b) 5
c) 6
1/4
d) 33
View Answer
Answer: d
Explanation: Execute in the shell to verify.
4. What will be the output of the following Python code?
1. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
2. v = values[0][0]
3. for lst in values:
4. for element in lst:
5. if v > element:
6. v = element
7. print(v)
a) 1
b) 3
c) 5
d) 6
View Answer
Answer: a
Explanation: Execute in the shell to verify.
5. What will be the output of the following Python code?
1. values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
2. for row in values:
3. row.sort()
4. for element in row:
5. print(element, end = " ")
6. 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
View Answer
Answer: d
Explanation: Execute in the shell to verify.
6. What will be the output of the following Python code?
2/4
1. matrix = [[1, 2, 3, 4],
2. [4, 5, 6, 7],
3. [8, 9, 10, 11],
4. [12, 13, 14, 15]]
5. for i in range(0, 4):
6. 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
Answer: d
Explanation: Execute in the shell to verify.
7. What will be the output of the following Python code?
1. def m(list):
2. v = list[0]
3. for e in list:
4. if v < e: v = e
5. return v
6. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
7. for row in values:
8. print(m(row), end = " ")
a) 3 33
b) 1 1
c) 5 6
d) 5 33
View Answer
Answer: d
Explanation: Execute in the shell to verify.
8. What will be the output of the following Python code?
1. data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
2. print(data[1][0][0])
3/4
a) 1
b) 2
c) 4
d) 5
View Answer
Answer: d
Explanation: Execute in the shell to verify.
9. What will be the output of the following Python code?
1. data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
2. def ttt(m):
3. v = m[0][0]
4. for row in m:
5. for element in row:
6. if v < element: v = element
7. return v
8. print(ttt(data[0]))
a) 1
b) 2
c) 4
d) 5
View Answer
Answer: c
Explanation: Execute in the shell to verify.
10. What will be the output of the following Python code?
1. points = [[1, 2], [3, 1.5], [0.5, 0.5]]
2. points.sort()
3. 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
Answer: c
4/4