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?
- [x ** 2 for x in range(1, 6)]
- [x ** 2 for x in range(5)]
- [x * 2 for x in range(1, 5)]
- [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, 2, 3, 4]
- [1, 4, 9, 16]
- [2, 4, 6, 8]
- [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?
- [x for x in range(11) if x % 2 == 0]
- [x for x in range(11) if x % 2 == 1]
- [x for x in range(10) if x % 2 == 0]
- [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)
- [x ** 2 for x in range(1, 4)]
- [x ** 2 for x in range(3)]
- [x * x for x in range(1, 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)
- List comprehensions are used to create new lists based on existing iterables.
- List comprehensions can include conditional statements.
- List comprehensions cannot be nested.
- 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:
- numbers = [-5, -3, 0, 2, 4]
- positive_numbers = [x for x in numbers if x > 0]
- 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:
- print(evens)
- [x for x in range(1, 11) if x % 2 == 0]
- 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:
- if x > 10]
- [x for x in numbers
- 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:
- first_letters = [word[0] for word in words]
- print(first_letters)
- 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, 2, 3, 4, 5]
- [3, 6, 9, 12, 15]
- [2, 4, 6, 8, 10]
- 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)
- [5, 6, 6]
- [5, 7, 6]
- [5, 7, 7]
- [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.
- True
- 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.
- True
- 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?
- It raises an error
- It returns an empty list
- It returns None
- 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?
- List comprehensions must always include a conditional statement.
- List comprehensions can be nested within each other.
- List comprehensions cannot perform operations on elements.
- 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?
- [x * 3 for x in range(10)]
- [x * 3 for x in range(1, 11)]
- [x * 3 for x in range(3, 30)]
- [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, 3, 5, 7]
- [2, 4, 6, 8]
- [3, 5, 7, 9]
- [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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics