w3resource

Constructing Vectors with Lists in Python Exam preparation

PCEP Certification Practice Test - Questions, Answers and Explanations

Below set of questions cover various aspects of constructing vectors (lists) in Python, aligned with the PCEP-30-02 1.1 certification topic.

Question 1: Which of the following is the correct way to create a vector in Python using a list?

  1. vector = (1, 2, 3)
  2. vector = {1, 2, 3}
  3. vector = [1, 2, 3]
  4. vector = 1, 2, 3

Answer: c) vector = [1, 2, 3]

Explanation: In Python, vectors are typically represented as lists. The correct syntax to create a list is by using square brackets []. Therefore, vector = [1, 2, 3] is the correct way to create a vector.

Question 2: What will be the output of the following code?

vector = [1, 2, 3]
print(len(vector))
  1. 3
  2. 2
  3. 1
  4. 4

Answer: a) 3

Explanation: The len() function returns the number of elements in the list. Since the list vector has 3 elements, len(vector) returns 3.

Question 3: Complete the following code to create a vector containing the elements 10, 20, and 30:

vector = [___, ___, ___]

Answer: vector = [10, 20, 30]

Explanation: The square brackets [] are used to define a list. Inside the brackets, the elements 10, 20, and 30 are separated by commas.

Question 4: What will the following code output?

vector = [1, 2, 3]
print(vector[1])
  1. 1
  2. 2
  3. 3
  4. IndexError

Answer: b) 2

Explanation: List indices in Python start at 0, so vector[1] accesses the second element in the list, which is 2.

Question 5: Which of the following correctly appends the value 4 to the vector vector = [1, 2, 3]?

  1. vector.append(4)
  2. vector.add(4)
  3. vector += [4]
  4. Both a) and c)

Answer: d) Both a) and c)

Explanation: The append() method adds an element to the end of the list. Alternatively, the += operator can be used to concatenate another list to the original list.

Question 6: Complete the code to print the last element of the vector:

vector = [1, 2, 3, 4]
print(vector[___])

Answer: -1

Explanation: In Python, negative indexing can be used to access elements from the end of the list. vector[-1] refers to the last element.

Question 7: Given the vector vector = [10, 20, 30], insert a value 40 at the end of the vector.

Answer: vector.append(40)

Explanation: The append() method adds an element to the end of the list.

Question 8: Which of the following are valid ways to create a list in Python? (Select all that apply)

  1. vector = [1, 2, 3]
  2. vector = list([1, 2, 3])
  3. vector = list(1, 2, 3)
  4. vector = []

Answer: a) vector = [1, 2, 3]
b) vector = list([1, 2, 3])
d) vector = []

Explanation: The list constructor list() can take an iterable as an argument, and square brackets [] are used to create a list directly. Option c) is incorrect because list() does not take multiple arguments without them being in an iterable like a tuple or another list.

Question 9: How do you access the third element of the vector vector = [5, 10, 15, 20]?

  1. vector(2)
  2. vector[2]
  3. vector[3]
  4. vector[1]

Answer: b) vector[2]

Explanation: List indices start at 0, so the third element is accessed with vector[2].

Question 10: Fill in the blanks to slice the vector vector = [1, 2, 3, 4, 5] to get the sublist [2, 3, 4]:

sub_vector = vector[__ : __]

Answer: sub_vector = vector[1 : 4]

Explanation: Slicing syntax vector[start:end] includes elements from index start to end-1. Therefore, vector[1:4] gives [2, 3, 4].

Question 11: Sort the following list in ascending order using the built-in method:

vector = [30, 10, 20, 40]
vector.___()
  1. sort
  2. sorted
  3. order

Answer: a) sort

Explanation: The sort() method sorts the list in place in ascending order.

Question 12: Insert a code snippet to create an empty vector in Python.

Answer: vector = []

Explanation: An empty list (or vector) can be created using empty square brackets [].

Question 13: Which of the following methods can be used to remove an element from a vector in Python? (Select all that apply)

  1. vector.remove(value)
  2. vector.pop(index)
  3. del vector[index]
  4. vector.delete(value)

Answer: a) vector.remove(value)
b) vector.pop(index)
c) del vector[index]

Explanation: remove() removes the first occurrence of the value, pop() removes the element at the specified index, and del deletes an element at the specified index. delete() is not a valid list method.

Question 14: Complete the code to add the elements of vector2 to the end of vector1:

vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
vector1 += ___

Answer: vector1 += vector2

Explanation: The += operator can be used to concatenate lists.

Question 15: What will be the output of the following code?

vector = [1, 2, 3]
print(vector[3])
  1. 3
  2. IndexError
  3. None
  4. 2

Answer: b) IndexError

Explanation: Accessing an index outside the range of the list will raise an IndexError.

Question 16: Fill in the blank to reverse the vector vector = [1, 2, 3, 4]:

vector.reverse()
print(vector)

Answer: Expected output: [4, 3, 2, 1]

Explanation: The reverse() method reverses the elements of the list in place.

Question 17: Complete the code to create a vector that contains the first 10 natural numbers.

vector = list(range(___, ___))

Answer: vector = list(range(1, 11))

Explanation: The range() function generates a sequence of numbers. range(1, 11) generates numbers from 1 to 10.

Question 18: Insert a code snippet to check if the value 5 exists in the vector vector = [1, 2, 3, 4, 5].

Answer:

if 5 in vector:
    print("5 is in the vector")

Explanation: The in operator checks if an element exists in a list.

Question 19: Sort the following code snippet to create a vector vector = [10, 20, 30] and then replace the second element with 25.

  1. a) vector[1] = 25 b) vector = [10, 20, 30]

Answer: b) vector = [10, 20, 30]
a) vector[1] = 25

Explanation: First, the vector is created. Then, the second element (index 1) is replaced with 25.

Question 20: Complete the code to iterate over the elements of vector = [1, 2, 3] and print each element:

for element in ___:
    print(element)

Answer:

		for element in vector:
    print(element)
		

Explanation: The for loop iterates over each element in the list.

Question 21: Insert a code snippet to extend the vector vector = [1, 2, 3] with another vector [4, 5, 6].

Answer: vector.extend([4, 5, 6])

Explanation: The extend() method appends elements from another iterable (in this case, a list) to the end of the list.

Question 22: Which of the following statements are true about lists in Python? (Select all that apply)

  1. Lists are mutable.
  2. Lists can contain elements of different data types.
  3. Lists are created using parentheses ().
  4. Lists can have duplicate elements.

Answer: a) Lists are mutable.
b) Lists can contain elements of different data types.
d) Lists can have duplicate elements.

Explanation: Lists are mutable, can contain elements of different data types, and can have duplicate elements. Lists are created using square brackets [], not parentheses.

Question 23: Fill in the blank to create a list containing only the even numbers from 2 to 10:

vector = [2, ___, 6, 8, 10]

Answer: vector = [2, 4, 6, 8, 10]

Explanation: The even numbers between 2 and 10 are 2, 4, 6, 8, 10.

Question 24: Complete the code to clear all elements from the vector vector = [1, 2, 3]:

vector.___()

Answer: vector.clear()

Explanation: The clear() method removes all elements from the list, resulting in an empty list.

Question 25: What does the following code do?

vector = [1, 2, 3]
vector[1:3] = [4, 5]
print(vector)
  1. Replaces the second and third elements with 4 and 5
  2. Appends 4 and 5 to the end of the list
  3. Inserts 4 and 5 at the beginning of the list
  4. Raises an error

Answer: a) Replaces the second and third elements with 4 and 5

Explanation: The slicing operation vector[1:3] = [4, 5] replaces the elements at index 1 and 2 with 4 and 5, resulting in [1, 4, 5].

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/data-collections-lists-constructing-vectors.php