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?

  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.
advertisement
advertisement

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?

advertisement
  1. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
  2.  
  3. v = values[0][0]
  4. for row in range(0, len(values)):
  5.     for column in range(0, len(values[row])):
  6.         if v < values[row][column]:
  7.             v = values[row][column]
  8.  
  9. print(v)

a) 3
b) 5
c) 6
d) 33
View Answer

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

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

  1. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
  2.  
  3. v = values[0][0]
  4. for lst in values:
  5.     for element in lst:
  6.         if v > element:
  7.             v = element
  8.  
  9. 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.  
  3. for row in values:
  4.     row.sort()
  5.     for element in row:
  6.         print(element, end = " ")
  7.     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?

  1. matrix = [[1, 2, 3, 4],
  2.        [4, 5, 6, 7],
  3.        [8, 9, 10, 11],
  4.        [12, 13, 14, 15]]
  5.  
  6. for i in range(0, 4):
  7.     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.  
  7. values = [[3, 4, 5, 1], [33, 6, 1, 2]]
  8.  
  9. for row in values: 
  10.     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.  
  3. print(data[1][0][0])

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.  
  3. def ttt(m):
  4.     v = m[0][0]
  5.  
  6.     for row in m:
  7.         for element in row:
  8.            if v < element: v = element
  9.  
  10.     return v
  11.  
  12. 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
Explanation: Execute in the shell to verify.

Sanfoundry Global Education & Learning Series – Python.

To practice all questions papers on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.