w3resource

PCEP Certification Practice Test: Constructing Strings

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 "constructing 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: Which of the following correctly constructs a string in Python?

  1. my_string = Hello, World!
  2. my_string = "Hello, World!"
  3. my_string = 'Hello', 'World!'
  4. my_string = Hello + World!

Answer: B) my_string = "Hello, World!"

Explanation: In Python, strings are enclosed in single (') or double (") quotes.

Question 2: Which of the following are valid ways to create a string in Python? (Choose all that apply)

  1. my_string = 'Python'
  2. my_string = "Python"
  3. my_string = '''Python'''
  4. my_string = """Python"""

Answer:

  1. my_string = 'Python'
  2. my_string = "Python"
  3. my_string = '''Python'''
  4. my_string = """Python"""

Explanation: Python allows strings to be created using single quotes, double quotes, or triple quotes (for multi-line strings).

Question 3: Complete the code to create a string that contains the text Hello, World!.

greeting = ______

Answer: "Hello, World!"

Explanation: The string "Hello, World!" is enclosed in double quotes.

Question 4: What will be the output of the following code?

text = "Python" + " " + "Programming"
print(text)
  1. PythonProgramming
  2. Python Programming
  3. Python+Programming
  4. Error

Answer: B) Python Programming

Explanation: The + operator concatenates the strings "Python", " ", and "Programming" to form "Python Programming".

Question 5: Insert the correct code to create a string full_name by joining the first name and last name with a space.

first_name = "John"
last_name = "Doe"
full_name = first_name + ______ + last_name

Answer: " "

Explanation: A space (" ") is added between first_name and last_name to correctly format full_name as "John Doe".

Question 6: Which of the following creates a multi-line string in Python?

  1. text = 'This is a\nmulti-line\nstring'
  2. text = '''This is a multi-line string'''
  3. text = 'This is a' + ' multi-line' + ' string'
  4. text = "This is a \multi-line \string"

Answer: B) text = '''This is a multi-line string'''

Explanation: Triple quotes (''' or """) are used to create multi-line strings in Python.

Question 7: Which of the following correctly construct a string that spans multiple lines? (Choose all that apply)

  1. multi_line = '''First line\nSecond line'''
  2. multi_line = """First line\nSecond line"""
  3. multi_line = 'First line' + '\n' + 'Second line'
  4. multi_line = "First line\nSecond line"

Answer: A) multi_line = '''First line\nSecond line'''
B) multi_line = """First line\nSecond line"""
C) multi_line = 'First line' + '\n' + 'Second line'
D) multi_line = "First line\nSecond line"

Explanation:All the options correctly create a string that spans multiple lines using either newline characters (\n) or triple quotes.

Question 8: Arrange the steps to correctly concatenate three strings: part1, part2, and part3.

  1. Use the + operator to concatenate the strings.
  2. Assign the concatenated result to a variable full_text.
  3. Create three separate strings: part1, part2, and part3

Answer: 3, 1, 2

Explanation: First, create the three separate strings, then concatenate them using the + operator, and finally assign the result to full_text.

Question 9: Complete the code to create a string that contains the text Python Programming.

language = "Python"
activity = "Programming"
result = ______ + " " + ______
print(result)

Answer: language, activity

Explanation: The variables language and activity are concatenated with a space in between to form the string "Python Programming".

Question 10: What will be the output of the following code?

name = "Alice"
message = "Hello, " + name + "!"
print(message)
  1. Hello Alice!
  2. Hello, Alice!
  3. Hello, + name + !
  4. Error

Answer: B) Hello, Alice!

Explanation: The code concatenates "Hello, ", name, and "!" to create the string "Hello, Alice!".

Question 11: Insert the correct code to create a string that repeats the word Python three times with spaces in between.

repeated_string = ("Python" + ______) * 3
print(repeated_string.strip())

Answer: " "

Explanation: The space " " is concatenated with "Python" and then repeated three times. The strip() method is used to remove any trailing spaces.

Question 12: Which of the following correctly creates a string by repeating the word "Code" five times?

  1. text = "Code" * 5
  2. text = "Code" + 5
  3. text = "Code" * "5"
  4. text = "Code" + "Code" + "Code" + "Code" + "Code"

Answer: A) text = "Code" * 5

Explanation: The * operator is used to repeat a string a specified number of times.

Question 13: Which of the following are valid string concatenations in Python? (Choose all that apply)

  1. result = "Hello" + " " + "World"
  2. result = "123" + str(456)
  3. result = "Sum: " + 123 + 456
  4. result = "Python" * 3

Answer:A) result = "Hello" + " " + "World"
B) result = "123" + str(456)
D) result = "Python" * 3

Explanation: Options A and B correctly concatenate strings, and Option D repeats the string "Python" three times. Option C would raise a TypeError because it attempts to concatenate strings and integers without converting them.

Question 14: Arrange the steps to correctly create a string greeting that says "Good Morning, Alice!".

  1. Concatenate "Good Morning, " with name.
  2. Assign "Alice" to a variable name.
  3. Assign the result to a variable greeting.

Answer: 2, 1, 3

Explanation: First, assign the value "Alice" to name, then concatenate it with "Good Morning, ", and finally assign the result to greeting

Question 15: Complete the code to create a string that contains the text "123456" by concatenating three separate strings.

part1 = "123"
part2 = "4"
part3 = "56"
result = part1 + ______ + part3
print(result)

Answer: part2

Explanation: The variables part1, part2, and part3 are concatenated to form the string "123456".

Question 16: What will be the output of the following code?

line1 = "Roses are red,"
line2 = "Violets are blue,"
line3 = "Sugar is sweet,"
line4 = "And so are you."

poem = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
print(poem)
  1. Roses are red,Violets are blue,Sugar is sweet,And so are you.
  2. Roses are red,\nViolets are blue,\nSugar is sweet,\nAnd so are you.
  3. Roses are red, (next line)Violets are blue, (next line)Sugar is sweet, (next line)And so are you.
  4. Roses are red, (same line)Violets are blue, (same line)Sugar is sweet, (same line)And so are you.

Answer: D) Roses are red, Violets are blue, Sugar is sweet, And so are you.

Explanation: The code concatenates the lines with newline characters \n, resulting in a multi-line string that is printed as Roses are red, Violets are blue, Sugar is sweet, And so are you.

17. Insert the correct code to construct a string that includes both single and double quotes, like "It's a "Python" class".

text = ______

Answer: 'It's a "Python" class'

Explanation: The string uses single quotes to enclose the entire text and an escape character \' to include the single quote in the word It's.

Question 18: What is the correct way to create an empty string in Python?

  1. empty_string = ""
  2. empty_string = ''
  3. empty_string = str()
  4. All of the above

Answer:D) All of the above

Explanation: All the options provided correctly create an empty string in Python.

Question 19: Which of the following are correct ways to construct a string using string interpolation or formatting? (Choose all that apply)

  • name = "Alice"; greeting = "Hello, {}".format(name)
  • name = "Alice"; greeting = "Hello, " + name + "!"
  • name = "Alice"; greeting = f"Hello, {name}!"
  • name = "Alice"; greeting = "Hello, {0}!".format(name)

Answer:

  • name = "Alice"; greeting = f"Hello, {name}!"
  • name = "Alice"; greeting = "Hello, {}".format(name)
  • name = "Alice"; greeting = "Hello, " + name + "!"
  • name = "Alice"; greeting = "Hello, {0}!".format(name)

Explanation: All the provided methods are valid ways to construct a string using string interpolation or concatenation in Python.

Question 20: Arrange the steps to create a string quote that includes a backslash (\) character.

  1. Assign the string to quote.
  2. Escape the backslash with another backslash.
  3. Write the string "This is a backslash: \\".

Answer: 3, 2, 1

Explanation: First, write the string including the escaped backslash, then assign the result to the variable quote.

Question 21: Complete the code to create a string that contains the text "He said, 'Hello!'".

quote = ______

Answer: 'He said, 'Hello!''

Explanation: The string is enclosed in single quotes, and the inner single quotes are escaped with a backslash.

Question 22: What will be the output of the following code?

title = "The title is: "
book = "The Great Gatsby"
result = title + book
print(result)
  1. The title is:The Great Gatsby
  2. The title is: The Great Gatsby
  3. Error
  4. The title is The Great Gatsby

Answer: B) The title is: The Great Gatsby

Explanation: The code concatenates the strings "The title is: " and "The Great Gatsby" to create the result "The title is: The Great Gatsby".

Question 23: Insert the correct code to create a multi-line string that displays:

Line 1
Line 2
Line 3
 
text = ______

Answer: '''Line 1\nLine 2\nLine 3'''

Explanation: Triple quotes are used to create a multi-line string with newline characters separating the lines.

Question 24: Which of the following is the most efficient way to create a string "Hello, World!" using Python's f-string formatting?

  1. name = "World"; text = f"Hello, {name}!"
  2. name = "World"; text = "Hello, {}!".format(name)
  3. name = "World"; text = "Hello, " + name + "!"
  4. text = "Hello, World!"

Answer: A) name = "World"; text = f"Hello, {name}!"

Explanation: Python's f-string formatting (Option A) is the most efficient way to create formatted strings as it is both concise and fast.

Question 25: Which of the following correctly creates a string that contains a newline character? (Choose all that apply)

  1. text = "First line\nSecond line"
  2. text = '''First line\nSecond line'''
  3. text = "First line" + "\n" + "Second line"
  4. text = "First line\\nSecond line"

Answer: A) text = "First line\nSecond line"
B) text = '''First line\nSecond line'''
C) text = "First line" + "\n" + "Second line"

Explanation: Options A, B, and C correctly create strings containing a newline character (\n). Option D would create a string with a literal backslash and n, not a newline



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://w3resource.com/python/certificate/data-collections-strings-constructing-strings.php