11. Multidimensional Lists - 17 Questions

# 11. Multidimensional Lists

17 Questions
Created by Y. Daniel Liang
Free
##### There are situations that demand multi-dimensional arrays or matrices. In many languages (Java, COBOL, BASIC) this notion of multi-dimensionality is handled by pre-declaring the dimensions (and limiting the sizes of each dimension). In Python, these are handled somewhat more simply. This quiz tests the knowledge of multidimensional lists in Python programming language.
1. What will be displayed by the following code?

m = [[123], [456], [789]]
print(m[0][0])

1
2
4
7

2. Assume m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], what are len(m)?

0
1
2
3

3. Assume m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], what are len(m[0])?

0
1
2
3

4. For m = [[x, x + 1, x + 2] for x in range(0, 3)], m is _______.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 1, 2, 3, 2, 3, 4]

5. For m = [[x, x + 1, x + 2] for x in range(1, 9, 3)], m is _______.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 1, 2, 3, 2, 3, 4]

6. How many elements are in m = [[x, y] for x in range(0, 4) for y in range(0, 4)]?

8
12
16
32

7. Assume x = ((1, 2), (3, 4, 5), (5, 6, 5, 9)), what are len(x) are len(x[0])?

2 and 1
2 and 2
3 and 2
2 and 3

8. Assume x = [[1, 2], [3, 4, 5], [5, 6, 5, 9]], what are len(x[0]), len(x[1]), and len(x[2])?

2, 3, and 3
2, 3, and 4
3, 3, and 3
3, 3, and 4

9. What will be displayed by the following program?

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)

3
5
6
33

10. What will be displayed by the following program?

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)

1
3
5
6

11. What will be displayed by the following program?

values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]

for row in values:

row.sort()

for element in row:

print(element, end = " ")

print()

The program prints two rows 3 4 5 1 followed by 33 6 1 2
The program prints on row 3 4 5 1 33 6 1 2
The program prints two rows 3 4 5 1 followed by 33 6 1 2
The program prints two rows 1 3 4 5 followed by 1 2 6 33

12. What will be displayed by the following 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 = " ")

1 2 3 4
4 5 6 7
1 3 8 12
2 5 9 13

13. What will be displayed by the following 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[1][i], end = " ")

1 2 3 4
4 5 6 7
1 3 8 12
2 5 9 13

14. What will be displayed by the following program?

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 = " ")

3 33
1 1
5 6
5 33

15. What will be displayed by the following code?

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

print(data[1][0][0])

1
2
4
5

16. What will be displayed the following 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]))

1
2
4
5

17. What will be displayed by the following code?

points = [[1, 2], [3, 1.5], [0.5, 0.5]]

points.sort()

print(points)

[[1, 2], [3, 1.5], [0.5, 0.5]]
[[3, 1.5], [1, 2], [0.5, 0.5]]
[[0.5, 0.5], [1, 2], [3, 1.5]]
[[0.5, 0.5], [3, 1.5], [1, 2]]