Understanding Strings and Variables in Python for PCEP-30-0x Exam
PCEP Certification Practice Test - Questions, Answers and Explanations
This comprehensive set of questions and explanations covers the fundamental topic of introducing literals and variables into code and using strings, providing a diverse set of interactions to enhance understanding and prepare for the PCEP-30-02 examination.
Question 1: Which of the following is a string literal in Python?
- 123
- True
- "Hello, World!"
- 3.14
Answer: C) "Hello, World!"
Explanation: "Hello, World!" is a string literal, while 123 is an integer, True is a Boolean, and 3.14 is a floating-point number.
Question 2: Which of the following represents a multi-line string literal in Python?
- "Hello\nWorld"
- 'Hello\nWorld'
- '''Hello\nWorld'''
- \"Hello\nWorld\"
Answer: C) '''Hello\nWorld'''
Explanation: Triple quotes (''' or """) are used for multi-line string literals in Python.
Question 3: Which of the following are valid variable names according to Python's naming conventions? (Select all that apply)
- _myVar
- my-var
- 2ndVar
- my_var
Answer: A) _myVar D) my_var
Explanation: _myVar and my_var are valid variable names according to Python's naming conventions. my-var is invalid because it contains a hyphen, and 2ndVar is invalid because it starts with a digit.
Question 4: Match the following string literals with their correct descriptions.
String Literals | Descriptions |
---|---|
"World" | 1. Multi-line string with single quotes |
'''Python''' | 2. Single-line string with single quotes |
'Hello' | 3. Single-line string with double quotes |
"""Programming""" | 4. Multi-line string with double quotes |
Answer:
- "World" -> 3. Single-line string with double quotes
- '''Python''' -> 1. Multi-line string with single quotes
- 'Hello' -> 2. Single-line string with single quotes
- """Programming""" -> 4. Multi-line string with double quotes
Explanation: Each string literal matches its respective description based on the type of quotes used.
Question 5: Fill in the blanks: In Python, string literals can be enclosed in ______, ______, ______, or ______ quotes.
▼Answer: In Python, string literals can be enclosed in single, double, triple single, or triple double quotes.
Explanation: Strings in Python can be enclosed in single ('), double ("), triple single ('''), or triple double (""") quotes.
Question 6: Arrange the following string literals in order based on the type of quotes used: single, triple double, double, triple single.
String Literals | Order |
---|---|
'''triple single''' | 1 |
"double" | 2 |
'single' | 3 |
"""triple double""" | 4 |
Answer:
- 'single'
- """triple double"""
- "double"
- '''triple single'''
Explanation: The order is based on the type of quotes used for each string literal.
Question 7: Fill in the code to assign the string literal "Hello, World!" to a variable named greeting.
greeting = _______
▼Answer: "Hello, World!"
Explanation: The variable greeting is assigned the string literal "Hello, World!".
Question 8: Insert the missing line of code to define a variable that holds the string literal 'Python'.
language = _______ print(language)▼
Answer: 'Python'
Explanation: The variable language is assigned the string literal 'Python'.
Question 9: What will be the output of the following code?
message = "Hello, World!" print(message)
- Hello, World!
- "Hello, World!"
- message
- print(message)
Answer: A) Hello, World!
Explanation: The variable message holds the string "Hello, World!", and print outputs it as Hello, World!.
Question 10: Which of the following is not a valid string literal in Python?
- 'Hello'
- "World"
- '''Python'''
- """Programming
Answer: D) """Programming
Explanation: Triple double quotes must be closed properly. Option D lacks the closing triple quotes.
Question 11: Which of the following statements about strings in Python are true? (Select all that apply)
- Strings are immutable.
- Strings can be concatenated using the + operator.
- Strings can be enclosed in either single or double quotes.
- Strings cannot contain numeric characters.
Answer: A) Strings are immutable. B) Strings can be concatenated using the + operator. C) Strings can be enclosed in either single or double quotes.
Explanation: Strings are immutable, can be concatenated with +, and can be enclosed in single or double quotes. Strings can contain numeric characters.
Question 12: Match the following string operations with their descriptions.
String Operations | Descriptions |
---|---|
str1 * 3 | 1. Length of the string |
len(str1) | 2. Access the first character |
str1[0] | 3. Concatenation |
str1 + str2 | 4. Repetition |
Answer:
- str1 * 3 -> 4. Repetition
- len(str1) -> 1. Length of the string
- str1[0] -> 2. Access the first character
- str1 + str2 -> 3. Concatenation
Explanation: Each string operation matches its respective description.
Question 13: Fill in the blanks: The + operator is used for ________ strings, while the * operator is used for ________ strings.
▼Answer: The + operator is used for concatenating strings, while the * operator is used for repeating strings.
Explanation: + concatenates strings, and * repeats them.
Question 14: Arrange the following operations in order to form a valid string manipulation sequence: concatenation, repetition, length calculation.
Operations | Order |
---|---|
str1 + str2 | 1 |
len(str4) | 2 |
str3 * 2 | 3 |
Answer:
- str1 + str2
- str3 * 2
- len(str4)
Explanation: The sequence is based on concatenation, repetition, and length calculation.
Question 15: Fill in the code to concatenate two strings, "Hello" and "World", and assign the result to a variable named greeting.
greeting = "Hello" + _______▼
Answer: "World"
Explanation: The variable greeting is assigned the concatenated string "Hello" + "World".
Question 16: Insert the missing line of code to define a variable that holds the string literal 'Python Programming'.
course = _______ print(course)▼
Answer: 'Python Programming'.
Explanation: The variable course is assigned the string literal 'Python Programming'.
Question 17: What will be the output of the following code?
text = "Python" print(text[0])
- P
- Python
- 0
- n
Answer: A) P
Explanation: text[0] accesses the first character of the string "Python", which is P.
Question 18: Which of the following methods is used to convert a string to uppercase in Python?
- upper()
- toupper()
- uppercase()
- toUpperCase()
Answer:A) upper()
Explanation: The upper() method is used to convert a string to uppercase.
Question 19: Which of the following string methods are used to modify the case of a string in Python? (Select all that apply)
- lower()
- upper()
- capitalize()
- title()
Answer: A) lower() B) upper() C) capitalize() D) title()
Explanation: All listed methods (lower(), upper(), capitalize(), title()) are used to modify the case of a string in Python.
Question 20: Match the following string methods with their functionalities.
String Methods | Functionalities |
---|---|
strip() | 1. Splits the string into a list |
replace() | 2. Replaces a substring with another |
split() | 3. Joins elements of a list into a string |
join() | 4. Removes leading and trailing spaces |
Answer:
- strip() -> 4. Removes leading and trailing spaces
- replace() -> 2. Replaces a substring with another
- split() -> 1. Splits the string into a list
- join() -> 3. Joins elements of a list into a string
Explanation: Each string method matches its respective functionality.
Question 21: Fill in the blanks: The split() method is used to ________ a string into a list, while the join() method is used to ________ elements of a list into a string.
▼Answer: The split() method is used to split a string into a list, while the join() method is used to join elements of a list into a string.
Explanation: split() splits a string into a list, and join() joins elements of a list into a string.
Question 22: Given the string methods split, strip, and join, describe their typical usage order in string processing.
▼Answer:
- strip()
- split()
- join()
Explanation: The sequence is based on first stripping spaces, then splitting the string into parts, and finally joining the parts back together.
Question 23: Fill in the code to convert the string "python" to uppercase and assign it to a variable named uppercase_text.
uppercase_text = "python"._______▼
Answer: upper()
Explanation: The upper() method converts the string "python" to uppercase.
Question 24: Insert the missing line of code to replace the substring "world" with "Python" in the string "Hello, world!".
text = "Hello, world!" text = text._______("world", "Python") print(text)▼
Answer: replace
Explanation: The replace method replaces the substring "world" with "Python".
Question 25: What will be the output of the following code?
sentence = "Learn Python Programming" words = sentence.split() print(words)
- ["Learn Python Programming"]
- ["Learn", "Python", "Programming"]
- ("Learn", "Python", "Programming")
- ["Learn", "Python"]
Answer: B) ["Learn", "Python", "Programming"]
Explanation: The split() method splits the string by spaces, resulting in a list of words.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics