PCEP Practice Test: Mastering Quotes and Apostrophes in Python 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 the subtopic "quotes and apostrophes inside 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 strings correctly includes both single and double quotes?
- text = 'She said, "It's raining."'
- text = 'She said, "It\'s raining."'
- text = "She said, "It's raining."
- text = 'She said, 'It's raining.'
Answer: B) text = 'She said, "It\'s raining."'
Explanation: Single quotes are used to enclose the string, allowing double quotes to be included directly. The single quote in "It's" is escaped using a backslash (\') to avoid ending the string early.
Question 2: Which of the following are valid ways to include both quotes and apostrophes in a string? (Choose all that apply)
- text = "It's \"Python\" time!"
- text = 'It\'s "Python" time!'
- text = "It's "Python" time!"
- text = 'It\'s \'Python\' time!'
Answer: A) text = "It's \"Python\" time!"
B) text = 'It\'s "Python" time!'
D) text = 'It\'s \'Python\' time!'
Explanation: Strings are immutable, meaning they cannot be modified in place. Concatenation and slicing create new strings.
Question 3: Complete the code to create a string that includes both single and double quotes: "It's a "Python" class!".
text = ______▼
Answer: 'It\'s a "Python" class!'
Explanation: The single quotes (' ') enclose the entire string. The apostrophe in It's is escaped with \'. The double quotes around "Python" are used directly without escaping because the string is enclosed in single quotes.
Question 4: What will be the output of the following code?
text = "She said, \"Python is fun!\"" print(text)
- She said, Python is fun!
- She said, \"Python is fun!\"
- She said, "Python is fun!"
- SyntaxError
Answer: C) She said, "Python is fun!"
Explanation: The backslashes escape the double quotes, so the string includes the quotes in the output.
Question 5: Insert the correct code to print the following string: I can't wait for the "Python" class!.
text = ______ print(text)▼
Answer: 'I can\'t wait for the "Python" class!'
Explanation: The string is enclosed in single quotes (' '). The apostrophe in "can't" is escaped with a backslash (\') to prevent it from ending the string early. The double quotes around "Python" are used directly because they are inside single quotes.
Question 6: Which of the following correctly includes a single quote in a double-quoted string?
- text = "It's a great day!"
- text = 'It\'s a great day!'
- text = 'It's a great day!'
- text = "It\'s a great day!"
Answer: A) text = "It's a great day!" D) text = "It\'s a great day!"
Explanation: In option A) the string is enclosed in double quotes, and the apostrophe in "It's" is allowed without escaping since it doesn't interfere with the double quotes and in option D) the string is enclosed in double quotes, and the apostrophe is unnecessarily escaped with a backslash. This works, but the escape is not needed in a double-quoted string.
Question 7: Which of the following strings are valid? (Choose all that apply)
- text = "He said, "Hello!""
- text = 'He said, "Hello!"'
- text = "He said, \"Hello!\""
- text = "He said, 'Hello!'"
Answer: B) text = 'He said, "Hello!"'
C) text = "He said, \"Hello!\""
D) text = "He said, 'Hello!'"
Explanation: Options B, C, and D are valid. Option A causes a syntax error because double quotes are not escaped or enclosed.
Question 8: Arrange the steps to correctly create a string that includes both single and double quotes.
- Assign the string to a variable.
- Choose single or double quotes to enclose the string.
- Escape any internal quotes as needed.
Answer: 2, 3, 1
Explanation: First, choose the appropriate quotes to enclose the string, then escape any internal quotes, and finally assign the string to a variable..
Question 9: Complete the code to create a string that includes the text He said, "It's Python time!".
text = ______▼
Answer: "He said, \"It's Python time!\""
Explanation: The string is enclosed in double quotes, and the internal double quotes are escaped.
Question 10: What will be the output of the following code?
text = 'It\'s "Python" class time!' print(text)
- It's "Python" class time!
- It\'s "Python" class time!
- Its "Python" class time!
- SyntaxError
Answer: A) It's "Python" class time!
Explanation: The apostrophe is escaped, allowing it to be included in the single-quoted string.
Question 11: Insert the correct code to create a string that includes both quotes and apostrophes: She said, "It's time to learn Python!".
text = ______ print(text)▼
Answer: "She said, \"It's time to learn Python!\""
Explanation: Double quotes are used to enclose the string, and the internal double quotes are escaped.
Question 12: Which of the following statements about quotes in strings is true?
- Single quotes cannot be used to create strings in Python.
- Double quotes cannot be used to create strings in Python.
- Triple quotes can be used to create strings that span multiple lines.
- Only double quotes can include single quotes inside the string.
Answer: C) Triple quotes can be used to create strings that span multiple lines.
Explanation: Triple quotes (''' or """) can be used to create multi-line strings in Python.
Question 13: Which of the following are correct ways to include both quotes and apostrophes in a string? (Choose all that apply)
- text = "She said, "Python is fun!""
- text = 'She said, "Python\'s fun!"'
- text = "She said, \"Python's fun!\""
- text = 'She said, \'Python\'s fun!\''
Answer: B) text = 'She said, "Python\'s fun!"'
C) text = "She said, \"Python's fun!\""
D) text = 'She said, \'Python\'s fun!\''
Explanation: Options B, C, and D correctly escape quotes and apostrophes as needed. Option A causes a syntax error.
Question 14: Arrange the steps to correctly include an apostrophe in a string.
- Use double quotes to enclose the string.
- Define the string.
- Print the string.
Answer: 1, 2, 3
Explanation: First, use double quotes to enclose the string, then define the string, and finally print the string.
Question 15: Complete the code to create a string that includes a single quote: Don't worry about it!.
text = ______▼
Answer: "Don't worry about it!"
Explanation: Double quotes are used to enclose the string, allowing the single quote to be included directly.
Question 16: What will be the output of the following code?
text = 'She said, "It\'s a wonderful day!"' print(text)
- She said, It's a wonderful day!
- She said, "It\'s a wonderful day!"
- She said, "It's a wonderful day!"
- SyntaxError
Answer: C) She said, "It's a wonderful day!"
Explanation: The single quote in "It's" is escaped, allowing the string to be valid and printed correctly.
17. Insert the correct code to include both single and double quotes in the string: The book is called "Python's Journey".
text = ______ print(text)▼
Answer: 'The book is called "Python\'s Journey"'
Explanation: Single quotes are used to enclose the string, and the apostrophe in "Python's" is escaped.
Question 18: What will be the result of the following code?
text = "It's called \"Python's Adventure\"" print(text)
- It's called "Python's Adventure"
- It\'s called "Python\'s Adventure"
- It's called Python's Adventure
- SyntaxError
Answer: A) It's called "Python's Adventure"
Explanation: The double quotes are escaped, allowing the string to include both single and double quotes.
Question 19: Which of the following strings will print correctly without causing an error? (Choose all that apply)
- text = "The title is \"Python's Guide\""
- text = 'It\'s a "Python" book'
- text = "It's a "Python" book"
- text = 'It\'s a \'Python\' book'
Answer: A) text = "The title is \"Python's Guide\""
B) text = 'It\'s a "Python" book'
D) text = 'It\'s a \'Python\' book'
Explanation: Options A, B, and D correctly handle both quotes and apostrophes. Option C causes a syntax error.
Question 20: Arrange the steps to correctly escape a double quote inside a string.
- Use double quotes to enclose the string.
- Print the string.
- Use a backslash to escape the internal double quote.
Answer: 1, 3, 2
Explanation: First, enclose the string in double quotes, then escape the internal double quote, and finally print the string.
Question 21: Complete the code to include a single quote in the string: It's a sunny day.
text = ______▼
Answer: "It's a sunny day"
Explanation: Double quotes are used to enclose the string, allowing the single quote to be included directly.
Question 22: What will be the output of the following code?
text = 'Python\'s "Best Practices"' print(text)
- Python's Best Practices
- Python's "Best Practices"
- Python\'s "Best Practices"
- SyntaxError
Answer: B) Python's "Best Practices"
Explanation: The apostrophe is escaped, allowing the string to be valid and printed correctly.
Question 23: Insert the correct code to create a string that includes an apostrophe and double quotes: "It's a "must-read" book".
text = ______ print(text)▼
Answer: '"It\'s a "must-read" book"'
Explanation: Single quotes are used to enclose the string, and the apostrophe in "It's" is escaped.
Question 24: Which of the following is the correct way to create a string that includes a single quote inside a single-quoted string?
- text = 'It\'s a beautiful day!'
- text = "It's a beautiful day!"
- text = 'It's a beautiful day!'
- text = "It\'s a beautiful day!"
Answer: A) text = 'It\'s a beautiful day!'
Explanation: The apostrophe is escaped with a backslash, allowing it to be included inside a single-quoted string.
Question 25: Which of the following correctly include both single and double quotes in a string? (Choose all that apply)
- text = 'He said, "It\'s a good day!"'
- text = "He said, \"It's a good day!\""
- text = "He said, "It's a good day!"
- text = 'He said, 'It's a good day!''
Answer: A) text = 'He said, "It\'s a good day!"'
B) text = "He said, \"It's a good day!\""
Explanation: Options A and B correctly escape the necessary quotes to include both single and double quotes in the string. Options C and D will raise syntax errors.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics