MCQ PRECTICE QUETION

1. What is the correct way to create a multiline string in Python?

a. multiline_string = 'This is a multiline string.'
b.
multiline_string = "This is a multiline string."
c.
multiline_string = '''This is a multiline string.'''
d.
multiline_string = """This is a multiline string."""

2. How do you access the last character of a string in Python?

a. string[-1]
b.
string[0]
c.
string[len(string) - 1]
d.
string.last()

3. What does the string.upper() method do?

a. Converts the string to lowercase
b. Converts the string to uppercase
c. Capitalizes the first letter of the string
d. Replaces a substring in the string

4. How do you check if a string contains a specific substring?

a. string.contains("substring")
b.
string.find("substring")
c.
string.has("substring")
d.
substring in string

5. What is the purpose of the string.split() method?

a. Splits the string into a list of characters
b. Splits the string into lines
c. Splits the string at a specified character and returns a list
d. Removes leading and trailing whitespaces from the string

6. How do you concatenate two strings in Python?

a. string1.concatenate(string2)
b.
string1 + string2
c.
concatenate(string1, string2)
d.
join(string1, string2)

7. What is the correct way to format a string with variables in Python?

a. formatted_string = "My name is {} and I am {} years old.".format(name, age)
b.
formatted_string = f"My name is {name} and I am {age} years old."
c.
formatted_string = "My name is %s and I am %d years old." % (name, age)
d. All of the above

8. How do you include a newline character in a string?

a. \n
b.
\\n
c.
/n
d.
newline()

9. What does the string.replace(old, new) method do?

a. Replaces the old string with a new one
b. Replaces all occurrences of a substring with a new string
c. Removes the old string from the string
d. Reverses the string

10. What is the correct way to check if a string starts with a specific prefix?

a. string.prefix("prefix")
b.
string.starts("prefix")
c.
string.startswith("prefix")
d.
prefix in string