PCEP Certification Practice Test: Using in and not in Operators
PCEP Certification Practice Test - Questions, Answers and Explanations
Here are 25 questions focusing on the topic "the in and not in operators" within the context of lists in Python for the PCEP-30-02 certification exam. The questions include various formats like single-select, multiple-select, gap fill, code insertion, sorting, and "rearrange" style questions, each with answers and explanations.
Question 1: Which of the following statements correctly checks if the element "apple" is in the list fruits?
- "apple" in fruits
- "apple" not in fruits
- if "apple" fruits
- if fruits in "apple"
Answer: a) "apple" in fruits
Explanation: The correct syntax to check for an element in a list is element in list. In this case, "apple" in fruits is the correct check.
Question 2: What will the following code output?
numbers = [1, 2, 3, 4, 5] if 3 in numbers: print("Found") else: print("Not Found")
- Found
- Not Found
- Error
- None
Answer: a) Found
Explanation: The in operator checks if 3 is present in the list numbers, which it is, so "Found" is printed.
Question 3: Which of the following is the correct way to check if the element "banana" is NOT in the list fruits?
- "banana" in fruits
- "banana" not fruits
- "banana" not in fruits
- fruits in "banana"
Answer: c) "banana" not in fruits
Explanation: The correct syntax to check if an element is NOT in a list is "element" not in list.
Question 4: Which of the following expressions will evaluate to True? (Choose all that apply)
- "cat" in ["dog", "cat", "mouse"]
- 5 not in [1, 2, 3, 4]
- 7 in [1, 2, 7, 8]
- "apple" not in ["banana", "grape"]
Answer: a) "cat" in ["dog", "cat", "mouse"], b) 5 not in [1, 2, 3, 4], c) 7 in [1, 2, 7, 8], d) "apple" not in ["banana", "grape"]
Explanation: In option a), "cat" is present in the list. In option b) 5 not in [1, 2, 3, 4]: This is true because 5 is not in the list. In option c), 7 is present in the list. In option d), "apple" is not in the list, so not in evaluates to True.
Question 5: Which of the following statements correctly uses the in and not in operators?
- if "x" in my_list: print("Exists")
- if not "x" in my_list: print("Doesn't Exist")
- if "x" not in my_list: print("Doesn't Exist")
- if my_list not in "x": print("Exists")
Answer: a) if "x" in my_list: print("Exists"), b) if not "x" in my_list: print("Doesn't Exist"), c) if "x" not in my_list: print("Doesn't Exist")
Explanation: Option a) checks if "x" is in the list, and option b) Checks if "x" is not in my_list, c) correctly checks if "x" is NOT in the list.
Question 6: The expression if __________ in my_list: is used to check if an element is present in the list.
▼Answer: element
Explanation: The correct syntax is if element in my_list: to check if element is present in the list.
Question 7: The expression if __________ not in my_list: is used to check if an element is absent from the list.
▼Answer: element
Explanation: The correct syntax is if element not in my_list: to check if element is NOT present in the list.
Question 8: Arrange the following code statements in the correct order to check if "grape" is in the list and print "Found" if it is:
- fruits = ["apple", "banana", "grape"]
- if "grape" in fruits:
- print("Found")
Answer: a) fruits = ["apple", "banana", "grape"]
b) if "grape" in fruits:
c) print("Found")
Explanation: The list is defined first, followed by the conditional check using in, and then the result is printed.
Question 9: Arrange the following code snippets in the correct order to check if the number 10 is not in the list and print "Not Found" if it isn’t:
- if 10 not in numbers:
- numbers = [1, 2, 3, 4, 5]
- print("Not Found")
Answer:
- numbers = [1, 2, 3, 4, 5]
- if 10 not in numbers:
- print("Not Found")
Explanation: The list is defined first, followed by the conditional check using not in, and then the result is printed.
Question 10: Complete the code to check if "orange" is in the list fruits and print "Exists" if it is:
fruits = ["apple", "banana", "cherry"] if __________: print("Exists")▼
Answer: "orange" in fruits
Explanation: The correct condition is "orange" in fruits to check for the presence of "orange" in the list.
Question 11: Complete the code to check if the number 7 is NOT in the list numbers and print "Missing" if it isn’t:
numbers = [2, 4, 6, 8, 10] if __________: print("Missing")▼
Answer: 7 not in numbers
Explanation: The correct condition is 7 not in numbers to check for the absence of 7 in the list.
Question 12: Insert the correct code to check if "kiwi" is in the list fruits and print "Present" if it is:
fruits = ["apple", "banana", "kiwi"] __________ print("Present")▼
Answer: if "kiwi" in fruits:
Explanation: The if "kiwi" in fruits: condition checks if "kiwi" is in the list and prints "Present" if it is.
Question 13: Insert the correct code to check if the number 15 is NOT in the list numbers and print "Not Found" if it isn’t:
numbers = [5, 10, 20, 25] __________ print("Not Found")▼
Answer: if 15 not in numbers:
Explanation: The if 15 not in numbers: condition checks if 15 is NOT in the list and prints "Not Found" if it isn’t.
Question 14: Rearrange the code to check if "strawberry" is NOT in the list and print "Missing" if it isn’t:
- print("Missing")
- fruits = ["apple", "banana", "kiwi"]
- if "strawberry" not in fruits:
Answer: b) fruits = ["apple", "banana", "kiwi"]
c) if "strawberry" not in fruits:
a) print("Missing")
Explanation: The list is defined first, followed by the conditional check using not in, and then the result is printed.
Question 15: Organize the steps to check if the number 25 is in the list and print "Found" if it is:
- if 25 in numbers:
- print("Found")
- numbers = [10, 15, 25, 35]
Answer: c) numbers = [10, 15, 25, 35]
a) if 25 in numbers:
b) print("Found")
Explanation: The list is created first, followed by the conditional check using in, and each element is printed.
Question 16: What does the following code output?
colors = ["red", "green", "blue"] if "yellow" in colors: print("Exists") else: print("Not Found")
- Exists
- Not Found
- Error
- None
Answer: b) Not Found
Explanation: The color "yellow" is not in the list, so the else block is executed, printing "Not Found".
Question 17: What does the following code output?
animals = ["dog", "cat", "bird"] if "dog" not in animals: print("Missing") else: print("Present")
- Present
- Missing
- Error
- None
Answer: a) Present
Explanation: The condition if "dog" not in animals: is false because "dog" is in the list, so the else block is executed.
Question 18: The expression "apple" in fruits returns True if "apple" is present in the list fruits.
- True
- False
Answer: a) True
Explanation: The in operator returns True if the element is in the list.
Question 19: The expression "banana" not in fruits returns True if "banana" is NOT present in the list fruits.
- True
- False
Answer: a) True
Explanation: The not in operator returns True if the element is absent from the list.
Question 20: What happens if you check for an element in an empty list using the in operator?
- The expression always returns False
- The expression raises an error
- The expression always returns True
- The expression checks only the first element
Answer: a) The expression always returns False
Explanation: An empty list contains no elements, so any in check will return False.
Question 21: Which of the following statements about the in and not in operators is correct?
- The in operator checks only the first element in a list.
- The in operator can be used with both lists and strings.
- The not in operator is slower than the in operator.
- The in operator checks for elements in reverse order.
Answer: b) The in operator can be used with both lists and strings.
Explanation: The in operator can be used with various data types, including lists and strings.
Question 22: Which of the following expressions would correctly check if an element is in the middle of a list?
- if "middle" in my_list[1:-1]:
- if "middle" not in my_list[1:-1]:
- if "middle" in my_list[::2]:
- if "middle" not in my_list[::-1]:
Answer: a) if "middle" in my_list[1:-1]:
Explanation: The slice my_list[1:-1] represents the middle portion of the list, excluding the first and last elements.
Question 23: Fill in the blank: The not in operator is used to check if an element is __________ from a list.
▼Answer: absent
Explanation: The not in operator checks if an element is absent from a list.
Question 24: The in operator is used to check if an element is __________ in a list.
▼Answer: present
Explanation: The in operator checks if an element is present in a list.
Question 25: What is the output of the following code?
letters = ["a", "b", "c", "d"] if "e" not in letters: print("Missing")
- Missing
- Found
- None
- Error
Answer: a) Missing
Explanation: The condition checks if "e" is absent from the list. Since "e" is not in the list, "Missing" is printed.
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics