PCEP Certification Practice Test: Indexing, Slicing, and String immutability
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 "indexing, slicing, immutability" for strings. 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 will be the output of the following code?
text = "Python" print(text[1])
- P
- y
- t
- h
Answer: B) y
Explanation: String indexing starts at 0, so text[1] returns the second character, which is 'y'.
Question 2: Which of the following statements about string immutability are true? (Choose all that apply)
- Strings cannot be modified after they are created.
- You can change a character in a string using indexing.
- Strings can be concatenated to form a new string.
- Slicing a string creates a new string
Answer: A) Strings cannot be modified after they are created.
C) Strings can be concatenated to form a new string.
D) Slicing a string creates a new string.
Explanation: Strings are immutable, meaning they cannot be modified in place. Concatenation and slicing create new strings.
Question 3: Complete the code to retrieve the last character of the string text.
text = "Hello" last_char = text[______]▼
Answer: -1
Explanation: Using an index of -1 accesses the last character in the string.
Question 4: What will be the output of the following code?
text = "Programming" print(text[3:6])
- ogr
- ram
- gra
- min
Answer: C) gra
Explanation: The slice text[3:6] returns the substring from index 3 up to but not including index 6, which is 'gra'.
Question 5: Insert the correct code to create a substring sub_text that contains the first three characters of the string text.
text = "Python" sub_text = text[______:______]▼
Answer: 0, 3
Explanation: The slice text[0:3] returns the first three characters of the string.
Question 6: What will be the result of the following code?
word = "immutable" word[0] = 'I' print(word)
- Immutable
- immutable
- iMMUTABLE
- TypeError
Answer: D) TypeError
Explanation: Strings in Python are immutable, so attempting to modify an element of a string using indexing will raise a TypeError.
Question 7: Which of the following are valid operations for slicing a string in Python? (Choose all that apply)
- text[:5]
- text[5:]
- text[1:5]
- text[::2]
Answer:
- text[:5]
- text[5:]
- text[1:5]
- text[::2]
Explanation:All the provided options are valid slicing operations. They can specify start, stop, and step parameters.
Question 8: Arrange the steps to correctly extract the middle three characters from the string word with an odd length.
- Calculate the middle index.
- Slice the string to get the middle three characters.
- Assign the string "abcdefg" to word.
Answer: 3, 1, 2
Explanation: First, assign "abcdefg" to word, calculate the middle index, then slice the string to get the middle three characters.
Question 9: Complete the code to create a string result that contains the reverse of the string text.
text = "Python" result = text[______]▼
Answer: ::-1
Explanation: The slice text[::-1] reverses the string.
Question 10: What will be the output of the following code?
text = "abcdefgh" print(text[2:7:2])
- cde
- ceg
- bdf
- cdg
Answer: B) ceg
Explanation: The slice text[2:7:2] starts at index 2, stops before index 7, and steps by 2, resulting in 'ceg'.
Question 11: Insert the correct code to slice the string text and retrieve every third character starting from the first character.
text = "1234567890" result = text[______]▼
Answer: ::3
Explanation: The slice text[::3] retrieves every third character starting from the first character.
Question 12: What will be the result of the following slicing operation?
text = "Python" print(text[-3:])
- Pyt
- hon
- tho
- on
Answer: B) hon
Explanation: The slice text[-3:] returns the last three characters of the string.
Question 13: Which of the following slicing operations will correctly extract the substring "world" from the string text = "Hello, world!"? (Choose all that apply)
- text[7:12]
- text[7:-1]
- text[-6:-1]
- text[-6:]
Answer:A) text[7:12]
C) text[-6:-1]
Explanation: Both text[7:12] and text[-6:-1] correctly extract "world" from the string. Option D include the last character, "!".
Question 14: Arrange the steps to correctly check if a substring exists within a string.
- Use the in operator to check for the substring.
- Define the string text.
- Print a message if the substring is found.
Answer: 2, 1, 3
Explanation: First, define the string, then use the in operator to check for the substring, and finally print a message if the substring is found.
Question 15: Complete the code to slice the string alphabet and obtain the substring "ghi".
alphabet = "abcdefghijklmnopqrstuvwxyz" substring = alphabet[______]▼
Answer: 6:9
Explanation: The slice alphabet[6:9] returns the substring "ghi" from the string.
Question 16: What will be the output of the following code?
text = "Data Science" print(text[:4])
- Data
- Sc
- Data
- D
Answer: A) Data
Explanation: The slice text[:4] returns the first four characters of the string, which are "Data".
17. Insert the correct code to slice the string text and remove the first and last characters.
text = "Hello, World!" result = text[______]▼
Answer: 1:-1
Explanation: The slice text[1:-1] removes the first and last characters from the string.
Question 18: Which of the following statements about string immutability is correct?
- Strings can be modified in place.
- Strings cannot be changed once created.
- You can replace characters in a string using the replace() method.
- String concatenation modifies the original string.
Answer: B) Strings cannot be changed once created.
Explanation: Strings are immutable, meaning they cannot be modified after creation. The replace() method creates a new string rather than modifying the original.
Question 19: Which of the following operations create a new string rather than modifying the existing one? (Choose all that apply)
- Slicing a string.
- Concatenating two strings.
- Using the replace() method.
- Accessing a string character by index.
Answer: A) Slicing a string.
B) Concatenating two strings.
C) Using the replace() method.
Explanation: Slicing, concatenating, and using the replace() method all create new strings, while accessing a character by index simply retrieves it without modification.
Question 20: Arrange the steps to correctly replace a substring in a string.
- Define the original string text.
- Use the replace() method to create a new string with the replacement.
- Print the new string.
Answer: 1, 2, 3
Explanation: First, define the original string, then use the replace() method to replace the substring, and finally print the new string.
Question 21: Complete the code to retrieve the first character of the string name.
name = "Python" first_char = name[______]▼
Answer: 0
Explanation: Index 0 retrieves the first character of the string.
Question 22: What will be the output of the following code?
sentence = "I love programming!" print(sentence[-11:-1])
- programming
- programmin
- programming!
- rogramming
Answer: D) rogramming
Explanation: The slice sentence[-11:-1] starts at the 11th character from the end and stops just before the last character, resulting in "rogramming".
Question 23: Insert the correct code to reverse the string word.
word = "immutable" reversed_word = word[______]▼
Answer: ::-1
Explanation: The slice word[::-1] reverses the string.
Question 24: What will be the result of the following slicing operation?
text = "OpenAI" print(text[1:4])
- Ope
- pen
- penA
- Open
Answer: B) pen
Explanation: The slice text[1:4] returns the substring from index 1 up to but not including index 4, which is "pen".
Question 25: Which of the following slicing operations will extract the substring "data" from the string text = "Big data analysis"? (Choose all that apply)
- text[4:8]
- text[-15:-11]
- text[4:-8]
- text[4:8] + text[-8:-4]
Answer: A) text[4:8]
C) text[4:-8]
Explanation: The slices text[4:8], and text[4:-8] all correctly extract "data" from the string.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics