Python String MCQ : Set 2

Python String MCQ

1). Which method is used to check if a string contains only printable characters?

a) isprintable()
b) isdisplayable()
c) isvisible()
d) isprintchar()

Correct answer is: a) isprintable()
Explanation: The isprintable() method checks if a string contains only printable characters.

2). What is the output of the following code?

text = “Hello World”
print(text.count(“l”))

a) 1
b) 3
c) 4
d) 9

Correct answer is: b) 3
Explanation: The count() method returns the number of occurrences of a substring in a string. In this case, “l” appears 3 times.

3). Which method is used to check if a string contains only alphanumeric characters?

a) isalphanumeric()
b) isalnum()
c) isalphanum()
d) isalphanumeric()

Correct answer is: b) isalnum()
Explanation: The isalnum() method checks if a string contains only alphanumeric characters.

4). What is the output of the following code?

text = “Hello World”
print(text.startswith(“Hello”))

a) True
b) False
c) None
d) Error

Correct answer is: a) True
Explanation: The startswith() method checks if a string starts with a specific substring.

5). Which method is used to convert a string to a list of characters?

a) to_list()
b) split()
c) explode()
d) list()

Correct answer is: d) list()
Explanation: The list() function converts a string to a list of characters.

6). What is the output of the following code?

text = “Sqatools!”
print(text.rstrip(“!”))

a) Sqatools!
b) Sqatools
c) Sqatools!!
d) Error

Correct answer is: b) Sqatools
Explanation: The rstrip() method removes trailing occurrences of a specified character from a string.

7). Which method is used to check if a string contains only decimal characters?

a) isdecimal()
b) isdec()
c) isdigit()
d) isnumber()

Correct answer is: a) isdecimal()
Explanation: The isdecimal() method checks if a string contains only decimal characters.

8). What is the output of the following code?

text = “Hello World”
print(text.index(“o”))

a) 4
b) 7
c) 9
d) Error

Correct answer is: a) 4
Explanation: The index() method returns the index of the first occurrence of the specified substring in the string. In this case, “o” appears at index 4.

9). Which method is used to check if a string contains only uppercase characters?

a) isupper()
b) isuppercase()
c) isupperchar()
d) isuc()

Correct answer is: a) isupper()
Explanation: The isupper() method checks if all characters in a string are uppercase.

10). What is the output of the following code?

text = “Sqatools”
print(text.isalnum())

a) True
b) False
c) None
d) Error

Correct answer is: b) False
Explanation: The isalnum() method checks if a string contains only alphanumeric characters.

11). Which method is used to check if a string contains only titlecased characters with the remaining characters being lowercase?

a) istitlecase()
b) istitle()
c) istitlelower()
d) istitlechar()

Correct answer is: b) istitle()
Explanation: The istitle() method checks if a string contains only titlecased characters with the remaining characters being lowercase.

12). Which method is used to check if a string contains only hexadecimal characters?

a) ishex()
b) ishexadecimal()
c) ishexchar()
d) isnumeric()

Correct answer is: a) ishex()
Explanation: The ishex() method checks if a string contains only hexadecimal characters.

13). Which method is used to insert a substring into a specific position in a string?

a) insert()
b) add()
c) place()
d) append()

Correct answer is: a) insert()
Explanation: The insert() method inserts a substring into a specific position in a string.

14). What is the output of the given code?

string = [“i”, “am”, “learning”, “python”]
temp = 0

for word in string:
    a = len(word)
    if a > temp:
        temp = a
print(temp)

a) 2
b) 3
c) 7
d) 8

Correct answer is: d) 8
Explanation: The given code finds the length of the longest word in the list “string” and assigns it to the variable “temp”. Finally, it prints the value of “temp”, which is 8 in this case.

15). What is the output of the following code?

string = “sqatoolspythonspy”
sub = “spy”
print(string.count(“spy”))

a) 0
b) 1
c) 2
d) 3

Correct answer is: c) 2
Explanation: The count() method returns the number of occurrences of a substring within a string. In this case, “spy” appears twice in the string.

16). What is the output of the given code snippet?

letter = “aerv”

for char in letter:
    if char == “a” or char == “e” or char == “i” or char == “o” or char == “u”:
        print(f”{char} is vowel”)
    else:
        print(f”{char} is consonant”)

a) a is vowel, e is vowel, r is consonant, v is consonant
b) a is consonant, e is consonant, r is consonant, v is consonant
c) a is vowel, e is vowel, r is vowel, v is consonant
d) a is vowel, e is consonant, r is consonant, v is consonant

Correct answer is: a) a is vowel, e is vowel, r is consonant, v is consonant
Explanation: The given code snippet iterates over each character in the string “letter”. For each character, it checks if it is equal to any of the vowels ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’. If it matches any of the vowels, it prints that the character is a vowel. Otherwise, it prints that the character is a consonant. In this case, ‘a’ and ‘e’ are vowels, while ‘r’ and ‘v’ are consonants.

17). Which method is used to split a string into a list of substrings?

a) split()
b) join()
c) replace()
d) find()

Correct answer is: a) split()
Explanation: The split() method is used to split a string into a list of substrings based on a specified delimiter. In this code, the delimiter is a space character.

18). What will be the output of the following code?

string = “we are learning python”
list1 = string.split(” “)
print(“Longest word: “, max(list1, key=len))

a) Longest word: learning
b) Longest word: we
c) Longest word: python
d) Longest word: are

Correct answer is: a) Longest word: learning
Explanation: The code splits the string into individual words and finds the word with the maximum length, which in this case is “learning”. It then prints “Longest word: learning”.

19). What is the output of the following code?

string = “we are learning python”
list1 = string.split(” “)
print(“Smallest word: “, min(list1, key=len))

a) Smallest word: we
b) Smallest word: python
c) Smallest word: learning
d) Smallest word: are

Correct answer is: a) Smallest word: we
Explanation: The code splits the string into a list of words using space as the delimiter. The min() function is used to find the smallest word based on the length of each word. In this case, “we” has the smallest length among all the words, so it is printed as the smallest word.

20). How does the code calculate the length of the string?

string = “im am learing python”
count = 0

for char in string:
    count +=1
print(“Length of the string using loop logic: “, count)

a) It uses the built-in function len() on the string.
b) It counts the number of words in the string.
c) It iterates over each character in the string and increments a counter.
d) It calculates the sum of the ASCII values of all characters in the string.

Correct answer is: c) It iterates over each character in the string and increments a counter.
Explanation: The code uses a loop to iterate over each character in the string. For each character encountered, the counter (count) is incremented by 1. After iterating over all characters, the value of count represents the length of the string.

21). What is the value of the variable result after executing the given code?

str1 = “Programming”
result = ”

for char in str1:
    if char in result:
        result = result + “$”
    else:
        result = result + char
print(“Result :”, result)

a) Programming
b) $rogramming
c) Pro$ram$in$
d) Pro$ramming

Correct answer is: c) Pro$ram$ing
Explanation: The code iterates through each character in the string str1. If the character is already present in the result string, it appends a “$” symbol to result. Otherwise, it appends the character itself. This process results in the modified string Pro$ram$in$.

22). Which operation is performed to concatenate the substrings in the code?

a) Addition (+)
b) Subtraction (-)
c) Multiplication (*)
d) Division (/)

Correct answer is: a) Addition (+)
Explanation: The concatenation operation is performed using the addition operator (+) in Python. In the code, the substrings are concatenated by using the + operator to create the final output string.

23). What is the output of the following code?

string = “SqaTool”
print(string[-1]+string[1:-1]+string[0])

a) “lqaTooS”
b) “lqaToolS”
c) “lqaTools”
d) “lqaToo”

Correct answer is: a) “lqaTooS”
Explanation: In this code, string[-1] retrieves the last character ‘l’, string[1:-1] retrieves the substring ‘qaToo’, and string[0] retrieves the first character ‘S’. By concatenating these substrings in the order of last character + middle substring + first character, we get the output “lqaTooS”.

24). What is the output of the following code?

string =”Its Online Learning”

list1 = string.split(” “)

for word in list1:
    new_word = word[-1]+word[1:-1]+word[0]
    index = list1.index(word)
    list1[index] = new_word

” “.join(list1)

a) Its Online Learning
b) sIt enilgnO gninraeL
c) stI enlinO gearninL
d) Learning Online Its

Correct answer is: c) stI enlinO gearninL
Explanation: The given code swaps the first and last characters of each word in the string “Its Online Learning” and returns the modified string as output. The words are separated by spaces.

25). What is the output of the following code?

string = “We are Learning Python Codding”
list1 = string.split(” “)
vowels = [“a”,”e”,”i”,”o”,”u”]
dictionary = dict()
for word in list1:
    count = 0
    for char in word:
        if char in vowels:
            count +=1
    dictionary[word] = count
print(dictionary)

a) {‘We’: 1, ‘are’: 2, ‘Learning’: 3, ‘Python’: 1, ‘Codding’: 2}
b) {‘We’: 1, ‘are’: 1, ‘Learning’: 2, ‘Python’: 1, ‘Codding’: 2}
c) {‘We’: 0, ‘are’: 1, ‘Learning’: 1, ‘Python’: 0, ‘Codding’: 1}
d) {‘We’: 1, ‘are’: 0, ‘Learning’: 1, ‘Python’: 0, ‘Codding’: 1}

Correct answer is: a) {‘We’: 1, ‘are’: 2, ‘Learning’: 3, ‘Python’: 1, ‘Codding’: 2}
Explanation: The code splits the string into a list of words, and for each word, it counts the number of vowels. The output is a dictionary where the keys are the words and the values are the vowel counts.

Leave a Comment