PCEP Practice Test: Basic String Functions and Methods in Python
PCEP Certification Practice Test - Questions, Answers and Explanations
Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "basic string functions and methods." The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: What is the result of the following code?
text = "hello" print(text.upper())
- HELLO
- hello
- Hello
- Error
Answer: A) HELLO
Explanation: The upper() method converts all characters in the string to uppercase.
Question 2: Which of the following methods return a string in all lowercase letters? (Choose all that apply)
- text.lower()
- text.casefold()
- text.capitalize()
- text.swapcase()
Answer: A) text.lower()
B) text.casefold()
Explanation: Both lower() and casefold() convert a string to lowercase. capitalize() only capitalizes the first letter, and swapcase() swaps the case of each character.
Question 3: Complete the code to find the index of the first occurrence of the substring "world" in the string text.
text = "Hello, world!" index = text.______("world") print(index)▼
Answer: find
Explanation: The find() method returns the index of the first occurrence of the specified substring. If the substring is not found, it returns -1.
Question 4: What will be the output of the following code?
text = "Hello, World!" print(text.replace("World", "Python"))
- Hello, Python!
- Hello, world!
- Hello,
- Python, World!
Answer: A) Hello, Python!
Explanation: The replace() method replaces the substring "World" with "Python", resulting in the output "Hello, Python!".
Question 5: Insert the correct method to remove leading and trailing whitespace from the string text.
text = " Hello, Python! " clean_text = text.______() print(clean_text)▼
Answer: strip
Explanation: The strip() method removes both leading and trailing whitespace from the string.
Question 6: Which of the following methods checks if a string ends with a specified substring?
- endswith()
- startswith()
- find()
- index()
Answer: A) endswith()
Explanation: The endswith() method checks if a string ends with the specified suffix.
Question 7: Which of the following methods can be used to remove specific characters from a string? (Choose all that apply)
- strip()
- replace()
- lstrip()
- rstrip()
Answer: A) strip()
B) replace()
C) lstrip()
D) rstrip()
Explanation: The strip(), lstrip(), and rstrip() methods can remove whitespace or specified characters. The replace() method can remove characters by replacing them with an empty string.
Question 8: Arrange the steps to correctly replace a substring in a string.
- Pass the old substring and the new substring as arguments.
- Call the replace() method.
- Assign the result to a new variable.
Answer: 2, 1, 3,
Explanation: First, call the replace() method, then pass the old and new substrings, and finally assign the result to a new variable.
Question 9: Complete the code to count the occurrences of the letter "o" in the string text.
text = "Hello, World!" count = text.______("o") print(count)▼
Answer: count
Explanation: The count() method returns the number of occurrences of a specified substring in a string.
Question 10: What will be the output of the following code?
text = "hello" print(text.capitalize())
- Hello
- HELLO
- hello
- Error
Answer: A) Hello
Explanation: The capitalize() method converts the first character to uppercase and the rest to lowercase.
Question 11: Insert the correct method to check if the string text starts with "Hello".
text = "Hello, World!" result = text.______("Hello") print(result)▼
Answer: startswith
Explanation: The startswith() method checks if a string starts with the specified prefix.
Question 12: What will be the result of the following code?
text = "Python programming" print(text.split())
- ['Python', 'programming']
- B) ['P', 'y', 't', 'h', 'o', 'n', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
- ['Python', 'programming', '']
- ['Python programming']
Answer: A) ['Python', 'programming']
Explanation: The split() method splits the string by whitespace into a list of words.
Question 13: Which of the following string methods modify the string by returning a new version of it? (Choose all that apply)
- upper()
- replace()
- split()
- index()
Answer: A) upper()
B) replace()
C) split()
Explanation: The upper(), replace(), and split() methods return a modified version of the string. The index() method returns the position of a substring, not a modified string.
Question 14: Arrange the steps to correctly split a sentence into words and store them in a list.
- Call the split() method.
- Print the list of words.
- Assign the result to a variable.
Answer: 1, 3, 2
Explanation: First, call the split() method, then assign the result to a variable, and finally print the list of words.
Question 15: Complete the code to check if the string text contains only digits.
text = "12345" result = text.______() print(result)▼
Answer: isdigit
Explanation: The isdigit() method checks if all characters in the string are digits.
Question 16: What will be the output of the following code?
text = " Hello, Python! " print(text.strip())
- Hello, Python!
- Hello, Python!
- Hello,Python!
- Error
Answer: A) Hello, Python!
Explanation: The strip() method removes leading and trailing whitespace from the string.
17. Insert the correct method to convert all characters in the string text to lowercase.
text = "PYTHON" lower_text = text.______() print(lower_text)▼
Answer: lower
Explanation: The lower() method converts all characters in a string to lowercase.
Question 18: Which of the following methods checks if a string contains only alphabetic characters?
- isalnum()
- isalpha()
- isdigit()
- isspace()
Answer: B) isalpha()
Explanation: The isalpha() method checks if all characters in the string are alphabetic.
Question 19: Which of the following methods can be used to check properties of a string? (Choose all that apply)
- isalpha()
- isalnum()
- isdigit()
- islower()
Answer: A) isalpha()
B) isalnum()
C) isdigit()
D) islower()
Explanation: All these methods are used to check specific properties of a string: isalpha() checks for alphabetic characters, isalnum() checks for alphanumeric characters, isdigit() checks for digits, and islower() checks for lowercase characters.
Question 20: Arrange the steps to check if a string contains only lowercase characters.
- Call the islower() method.
- Assign the string to a variable.
- Print the result of the islower() method.
Answer: 2, 1, 3
Explanation: First, assign the string to a variable, then call the islower() method, and finally print the result.
Question 21: Complete the code to check if the string text contains only alphabetic characters and digits.
text = "Python3" result = text.______() print(result)▼
Answer: isalnum
Explanation: The isalnum() method checks if the string contains only alphanumeric characters (letters and digits).
Question 22: What will be the output of the following code?
text = "HELLO" print(text.isupper())
- True
- False
- HELLO
- Error
Answer: A) True
Explanation: The isupper() method checks if all characters in the string are uppercase, and since they are, the result is True.
Question 23: Insert the correct method to convert the first character of the string text to uppercase and the rest to lowercase.
text = "python" result = text.______() print(result)▼
Answer: capitalize
Explanation: The capitalize() method converts the first character of the string to uppercase and the remaining characters to lowercase.
Question 24: What will be the result of the following code?
text = "Python Programming" print(text.find("Pro"))
- -1
- 7
- 5
- Error
Answer: B) 7
Explanation: The find() method returns the index of the first occurrence of the substring "Pro" in the string, which starts at index 7.ne breaks.
Question 25: Which of the following methods return a boolean value? (Choose all that apply)
- startswith()
- endswith()
- islower()
- find()
Answer: A) startswith()
B) endswith()
C) islower()
Explanation: The startswith(), endswith(), and islower() methods return boolean values (True or False). The find() method returns the index of a substring.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics