w3resource

PCEP Certification Practice Test: List Comprehensions

PCEP Certification Practice Test - Questions, Answers and Explanations

Here are 25 questions focusing on the topic of "list comprehensions" within the context of Python lists for the PCEP-30-0x certification exam. The questions include various formats such as single-select, multiple-select, gap fill, code insertion, sorting, and "rearrange" style questions. Each question is followed by the correct answer and an explanation.

Question 1: Which of the following correctly uses list comprehension to create a list of squares for the numbers 1 through 5?

  1. [x ** 2 for x in range(1, 6)]
  2. [x ** 2 for x in range(5)]
  3. [x * 2 for x in range(1, 5)]
  4. [x for x in range(1, 6)]

Answer: a) [x ** 2 for x in range(1, 6)]

Explanation: The correct syntax for list comprehension involves using the expression x ** 2 to get squares, with the range from 1 to 5 (inclusive).

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

numbers = [1, 2, 3, 4]
squares = [x * x for x in numbers]
print(squares)
  1. [1, 2, 3, 4]
  2. [1, 4, 9, 16]
  3. [2, 4, 6, 8]
  4. [1, 2, 3, 4, 5]

Answer: b) [1, 4, 9, 16]

Explanation: The list comprehension generates the squares of each number in the list [1, 2, 3, 4].

Question 3: Which of the following list comprehensions creates a list of even numbers from 0 to 10?

  1. [x for x in range(11) if x % 2 == 0]
  2. [x for x in range(11) if x % 2 == 1]
  3. [x for x in range(10) if x % 2 == 0]
  4. [x for x in range(12) if x % 3 == 0]

Answer: a) [x for x in range(11) if x % 2 == 0]

Explanation: The list comprehension filters for even numbers using x % 2 == 0 within the range of 0 to 10.

Question 4: Which of the following list comprehensions generate a list of squares for numbers from 1 to 3? (Choose two)

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

Answer: a) [x ** 2 for x in range(1, 4)], c) [x * x for x in range(1, 4)]

Explanation: Both a) and c) correctly calculate the squares of numbers 1 through 3 using list comprehension.

Question 5: Which of the following statements about list comprehensions are correct? (Choose all that apply)

  1. List comprehensions are used to create new lists based on existing iterables.
  2. List comprehensions can include conditional statements.
  3. List comprehensions cannot be nested.
  4. List comprehensions always result in a list with the same length as the original iterable.

Answer: a) List comprehensions are used to create new lists based on existing iterables., b) List comprehensions can include conditional statements.

Explanation: List comprehensions are a concise way to create lists and can include conditions. They can also be nested, and they don't always generate lists with the same length as the original iterable.

Question 6: The expression [x * 2 for x in range(5)] generates a list of __________.

Answer: doubled values of numbers 0 to 4

Explanation: The list comprehension multiplies each number in the range 0 to 4 by 2.

Question 7: List comprehensions can include conditions using the __________ keyword to filter elements.

Answer: if

Explanation: The if keyword can be used in list comprehensions to include only elements that meet a certain condition.

Question 8: Arrange the following code statements in the correct order to create a list comprehension that filters only positive numbers from a list:

  1. numbers = [-5, -3, 0, 2, 4]
  2. positive_numbers = [x for x in numbers if x > 0]
  3. print(positive_numbers)

Answer: a) numbers = [-5, -3, 0, 2, 4]
b) positive_numbers = [x for x in numbers if x > 0]
c) print(positive_numbers)

Explanation: The list is defined first, followed by the list comprehension that filters positive numbers, and finally, the result is printed.

Question 9: Arrange the following code snippets in the correct order to generate a list of even numbers from 1 to 10 using list comprehension:

  1. print(evens)
  2. [x for x in range(1, 11) if x % 2 == 0]
  3. evens =

Answer: c) evens =
b) [x for x in range(1, 11) if x % 2 == 0]
a) print(evens)

Explanation: The list comprehension is assigned to evens, and then the result is printed.

Question 10: Complete the code to create a list comprehension that doubles the numbers in the list numbers:

numbers = [1, 2, 3, 4]
doubled = [__________]

Answer: x * 2 for x in numbers

Explanation: The list comprehension multiplies each element by 2.

Question 11: Complete the code to generate a list of numbers from 1 to 10 that are divisible by 3:

multiples_of_three = [__________]
print(multiples_of_three)

Answer: x for x in range(1, 11) if x % 3 == 0

Explanation: The list comprehension filters for numbers that are divisible by 3 within the range 1 to 10.

Question 12: Insert the correct code to create a list comprehension that converts all strings in the list words to uppercase:

words = ["hello", "world"]
uppercase_words = __________

Answer: [word.upper() for word in words]

Explanation: The list comprehension uses .upper() to convert each string to uppercase.

Question 13: Insert the correct code to generate a list of squares for even numbers from 1 to 10:

squares = __________

Answer: [x ** 2 for x in range(1, 11) if x % 2 == 0]

Explanation: The list comprehension squares only the even numbers within the range.

Question 14: Rearrange the code to create a list comprehension that filters only the numbers greater than 10 from a list:

  1. if x > 10]
  2. [x for x in numbers
  3. numbers = [5, 12, 8, 20, 15]

Answer: c) numbers = [5, 12, 8, 20, 15]
b) [x for x in numbers
a) if x > 10]

Explanation: The list is defined first, then the list comprehension filters elements greater than 10.

Question 15: Organize the steps to generate a list comprehension that extracts the first letter of each word in the list:

  1. first_letters = [word[0] for word in words]
  2. print(first_letters)
  3. words = ["apple", "banana", "cherry"]

Answer: c) words = ["apple", "banana", "cherry"]
a) first_letters = [word[0] for word in words]
b) print(first_letters)

Explanation: The list is created first, followed by the list comprehension that extracts the first letter, and finally, the result is printed.

Question 16: What does the following code output?

numbers = [1, 2, 3, 4, 5]
result = [x * 3 for x in numbers]
print(result)
  1. [1, 2, 3, 4, 5]
  2. [3, 6, 9, 12, 15]
  3. [2, 4, 6, 8, 10]
  4. None

Answer: b) [3, 6, 9, 12, 15]

Explanation: The list comprehension multiplies each number by 3.

Question 17: What does the following code output?

words = ["apple", "banana", "cherry"]
lengths = [len(word) for word in words]
print(lengths)
  1. [5, 6, 6]
  2. [5, 7, 6]
  3. [5, 7, 7]
  4. [5, 6, 7]

Answer: a) [5, 6, 6]

Explanation: The list comprehension calculates the length of each word in the list.

Question 18: List comprehensions can only be used with numbers.

  1. True
  2. False

Answer: b) False

Explanation: List comprehensions can be used with any iterable, including strings, lists, and more.

Question 19: A list comprehension can include both an if and else condition.

  1. True
  2. False

Answer: a) True

Explanation: List comprehensions can include both if and else to create different outputs based on conditions.

Question 20: What happens if you use list comprehension on an empty list?

  1. It raises an error
  2. It returns an empty list
  3. It returns None
  4. It returns a list of None values

Answer: b) It returns an empty list

Explanation: Applying list comprehension to an empty list results in an empty list, as there are no elements to process.

Question 21: Which of the following statements about list comprehensions is correct?

  1. List comprehensions must always include a conditional statement.
  2. List comprehensions can be nested within each other.
  3. List comprehensions cannot perform operations on elements.
  4. List comprehensions can only be used with numerical data.

Answer: b) List comprehensions can be nested within each other.

Explanation: List comprehensions can be nested to generate more complex lists.

Question 22: Which of the following expressions would correctly generate a list of the first 10 multiples of 3?

  1. [x * 3 for x in range(10)]
  2. [x * 3 for x in range(1, 11)]
  3. [x * 3 for x in range(3, 30)]
  4. [x + 3 for x in range(1, 11)]

Answer: b) [x * 3 for x in range(1, 11)]

Explanation: The list comprehension [x * 3 for x in range(1, 11)] correctly generates the first 10 multiples of 3.

Question 23: List comprehensions provide a concise way to __________ elements from an existing iterable.

Answer: transform

Explanation: List comprehensions are often used to transform elements from an existing iterable based on certain operations.

Question 24: The structure of a basic list comprehension is [expression for item in __________].

Answer: iterable

Explanation: The basic structure is [expression for item in iterable], where iterable is the source data being processed.

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

nums = [2, 4, 6, 8]
result = [x - 1 for x in nums]
print(result)
  1. [1, 3, 5, 7]
  2. [2, 4, 6, 8]
  3. [3, 5, 7, 9]
  4. [1, 2, 3, 4]

Answer: a) [1, 3, 5, 7]

Explanation: The list comprehension subtracts 1 from each element in the list.

Test your Python skills with w3resource's quiz



Become a Patron!

Follow us on Facebook and Twitter for latest update.