w3resource

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])
  1. P
  2. y
  3. t
  4. 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)

  1. Strings cannot be modified after they are created.
  2. You can change a character in a string using indexing.
  3. Strings can be concatenated to form a new string.
  4. 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])
  1. ogr
  2. ram
  3. gra
  4. 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)
  1. Immutable
  2. immutable
  3. iMMUTABLE
  4. 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)

  1. text[:5]
  2. text[5:]
  3. text[1:5]
  4. text[::2]

Answer:

  1. text[:5]
  2. text[5:]
  3. text[1:5]
  4. 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.

  1. Calculate the middle index.
  2. Slice the string to get the middle three characters.
  3. 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])
  1. cde
  2. ceg
  3. bdf
  4. 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:])
  1. Pyt
  2. hon
  3. tho
  4. 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)

  1. text[7:12]
  2. text[7:-1]
  3. text[-6:-1]
  4. 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.

  1. Use the in operator to check for the substring.
  2. Define the string text.
  3. 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])
  1. Data
  2. Sc
  3. Data
  4. 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?

  1. Strings can be modified in place.
  2. Strings cannot be changed once created.
  3. You can replace characters in a string using the replace() method.
  4. 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.

  1. Define the original string text.
  2. Use the replace() method to create a new string with the replacement.
  3. 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])
  1. programming
  2. programmin
  3. programming!
  4. 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])
  1. Ope
  2. pen
  3. penA
  4. 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)

  1. text[4:8]
  2. text[-15:-11]
  3. text[4:-8]
  4. 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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python/certificate/data-collections-strings-indexing-slicing-immutability.php