w3resource

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?

  1. 123
  2. True
  3. "Hello, World!"
  4. 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?

  1. "Hello\nWorld"
  2. 'Hello\nWorld'
  3. '''Hello\nWorld'''
  4. \"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)

  1. _myVar
  2. my-var
  3. 2ndVar
  4. 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)
  1. Hello, World!
  2. "Hello, World!"
  3. message
  4. 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?

  1. 'Hello'
  2. "World"
  3. '''Python'''
  4. """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)

  1. Strings are immutable.
  2. Strings can be concatenated using the + operator.
  3. Strings can be enclosed in either single or double quotes.
  4. 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:

  1. str1 * 3 -> 4. Repetition
  2. len(str1) -> 1. Length of the string
  3. str1[0] -> 2. Access the first character
  4. 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])
  1. P
  2. Python
  3. 0
  4. 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?

  1. upper()
  2. toupper()
  3. uppercase()
  4. 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)

  1. lower()
  2. upper()
  3. capitalize()
  4. 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:

  1. strip()
  2. split()
  3. 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)
  1. ["Learn Python Programming"]
  2. ["Learn", "Python", "Programming"]
  3. ("Learn", "Python", "Programming")
  4. ["Learn", "Python"]

Answer: B) ["Learn", "Python", "Programming"]

Explanation: The split() method splits the string by spaces, resulting in a list of words.



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/literals-and-variables-strings.php