w3resource

PCEP Practice Test: Lists in Lists - Matrices and Cubes

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions focusing on the topic "lists in lists: matrices and cubes" for the PCEP-30-02 certification exam. The questions include various formats such as single-select, multiple-select, gap fill, code insertion, sorting, and rearranging style questions. Each question is followed by the correct answer and an explanation.

Question 1: What is a matrix in Python?

  1. A list with multiple data types
  2. A list containing other lists as elements
  3. A function for generating numbers
  4. A method to sort lists

Answer: b) A list containing other lists as elements

Explanation: A matrix in Python is commonly represented as a list of lists, where each sub-list represents a row.

Question 2: How do you access the element in the second row and third column of a 2D list named matrix?

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  1. matrix[1][2]
  2. matrix[2][1]
  3. matrix[3][2]
  4. matrix[1, 2]

Answer: a) matrix[1][2]

Explanation: Indexing in Python starts at 0, so matrix[1][2] accesses the second row and third column, which is 6.

Question 3: What is the correct way to initialize a 3x3 matrix with all zeroes?

  1. matrix = [[0 for i in range(3)] for j in range(3)]
  2. matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  3. matrix = [[0] * 3 for i in range(3)]
  4. All of the above

Answer: d) All of the above

Explanation: All options correctly create a 3x3 matrix filled with zeroes.

Question 4: Which of the following are valid ways to access elements in a 2D list? (Choose all that apply)

  1. matrix[1][2]
  2. matrix[0][0]
  3. matrix[2, 1]
  4. matrix[1]

Answer: a) matrix[1][2], b) matrix[0][0], d) matrix[1]

Explanation: Accessing elements in a 2D list requires double indexing like matrix[1][2] or single indexing like matrix[1] to get an entire row. Option c) uses incorrect syntax.

Question 5: Which of the following code snippets correctly create a 2x2 matrix? (Choose two)

  1. matrix = [[1, 2], [3, 4]]
  2. matrix = [1, 2, 3, 4]
  3. matrix = [[1, 2], [3, 4], [5, 6]]
  4. matrix = [[1, 2], [3, 4]] * 2

Answer: a) matrix = [[1, 2], [3, 4]], d) matrix = [[1, 2], [3, 4]] * 2.

Explanation: Both a) and d) create a valid 2x2 matrix. Option b) is a 1D list, and option c) is a 3x2 matrix.

Question 6: A 3D list in Python can be thought of as a __________ of 2D matrices.

Answer: collection

Explanation: A 3D list is essentially a collection of 2D matrices, where each element in the 3D list is a matrix.

Question 7: The expression cube[1][2][0] accesses an element from a __________ list.

Answer: 3D

Explanation: The expression involves three levels of indexing, indicating it accesses an element from a 3D list (a list of lists of lists).

Question 8: Arrange the following code statements in the correct order to create a 3x3 identity matrix:

  1. identity_matrix = [[0, 0, 0], [0, 1, 0], [0, 0, 1]]
  2. print(identity_matrix)
  3. identity_matrix = [[1 if i == j else 0 for j in range(3)] for i in range(3)]

Answer: c) identity_matrix = [[1 if i == j else 0 for j in range(3)] for i in range(3)]
b) print(identity_matrix)

Explanation: The identity matrix is created using list comprehension, where 1s are placed along the diagonal. Then it is printed.

Question 9: Arrange the following code snippets in the correct order to initialize a 2x2x2 cube with all elements set to 1:

  1. print(cube)
  2. cube = [[[1 for k in range(2)] for j in range(2)] for i in range(2)]

Answer: b) cube = [[[1 for k in range(2)] for j in range(2)] for i in range(2)]
a) print(cube)

Explanation: The 3D list (cube) is created first using nested list comprehensions, then it is printed.

Question 10: Complete the code to create a 4x4 matrix with all elements set to 7:

matrix = [[__________ for j in range(4)] for i in range(4)]

Answer: 7

Explanation: The list comprehension [7 for j in range(4)] generates a row of 7s, and the outer loop repeats this row 4 times.

Question 11: Complete the code to create a 2x2 matrix using nested list comprehensions:

matrix = [[__________] for i in range(2)]

Answer: [0, 1]

Explanation: The inner list [0, 1] creates a row, and the outer loop repeats this row 2 times to form a 2x2 matrix.

Question 12: Insert the correct code to create a 3D list (2x2x2 cube) with all elements initialized to 0:

cube = __________

Answer: [[[0 for k in range(2)] for j in range(2)] for i in range(2)]

Explanation: The code uses three nested list comprehensions to create a 2x2x2 cube where each element is 0.

Question 13: Insert the correct code to access the element in the third row and second column of a 4x4 matrix named matrix:

element = __________

Answer: matrix[2][1]

Explanation: In Python, indexing starts at 0, so matrix[2][1] accesses the third row and second column.

Question 14: Rearrange the code to create a 3x3 matrix where each element is the sum of its row and column indices:

  1. print(matrix)
  2. for i in range(3):
  3. matrix = [[i + j for j in range(3)] for i in range(3)]

Answer: c) matrix = [[i + j for j in range(3)] for i in range(3)]
a) print(matrix)

Explanation: The matrix is created first using list comprehension, where each element is the sum of its row and column indices.

Question 15: Organize the steps to initialize a 4x4 matrix with each element set to its row index:

  1. print(matrix)
  2. matrix = [[i for j in range(4)] for i in range(4)]

Answer: b) matrix = [[i for j in range(4)] for i in range(4)]
a) print(matrix)

Explanation: The matrix is created using list comprehension where each element in a row is set to the row index i.

Question 16: What does the following code output?

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[2][0])
  1. 1
  2. 7
  3. 3
  4. 3

Answer: b) 7

Explanation: The code accesses the first element of the third row, which is 7.

17. What does the following code output?

cube = [[[1 for k in range(2)] for j in range(2)] for i in range(2)]
print(cube[1][0][1])
  1. 1
  2. 0
  3. 2
  4. None

Answer: a) 1

Explanation: The code accesses the element at the specified indices in a 2x2x2 cube, which is 1.

Question 18: A 2D list in Python is represented as a list containing other lists.

  1. True
  2. False

Answer: a) True

Explanation: A 2D list is a list where each element is itself a list, representing rows of a matrix.

Question 19: Accessing an element in a 3D list requires three levels of indexing.

  1. True
  2. False

Answer: a) True

Explanation: In a 3D list (a list of lists of lists), three indices are needed to access a specific element.

Question 20: What happens if you try to access an element that is out of bounds in a matrix?

  1. It returns None
  2. It raises an IndexError
  3. It returns an empty list
  4. It raises a TypeError

Answer: b) It raises an IndexError

Explanation: Accessing an index that does not exist in a list results in an IndexError.

Question 21: Which of the following statements about matrices and cubes is correct?

  1. A matrix can only have numeric data.
  2. A cube is a 3D list, with lists nested three levels deep.
  3. List comprehensions cannot be used to generate matrices.
  4. Python does not support matrices or cubes natively.

Answer: b) A cube is a 3D list, with lists nested three levels deep.

Explanation: A cube is a list of lists of lists, making it a 3D structure in Python.

Question 22: Which of the following expressions correctly creates a 2x3 matrix?

  1. [[1, 2, 3] for _ in range(2)]
  2. [[1, 2] for _ in range(3)]
  3. [[1, 2, 3]] * 2
  4. [[1, 2, 3, 4] for _ in range(2)]

Answer: a) [[1, 2, 3] for _ in range(2)]

Explanation: The list comprehension [[1, 2, 3] for _ in range(2)] correctly creates a 2x3 matrix.

Question 23: A 3D list can be visualized as a __________ of 2D matrices.

Answer: stack

Explanation: A 3D list can be visualized as a stack of 2D matrices, where each matrix is a layer in the 3D structure.

Question 24: The expression matrix[1][2] accesses the element in the second __________ and third column of the matrix.

Answer: row

Explanation: The first index represents the row, and the second index represents the column.

Question 25: What is the output of the following code?

matrix = [[10, 20], [30, 40]]
print(matrix[1][1])
  1. 10
  2. 20
  3. 30
  4. 40

Answer: d) 40

Explanation: The code accesses the second row and second column of the matrix, which contains the value 40.



Become a Patron!

Follow us on Facebook and Twitter for latest update.