Python MCQs on String: SET 12

111. What will be the output of the following Python code snippet?

print(‘cd’.partition(‘cd’))

a) (‘cd’)

b) (”)

c) (‘cd’, ”, ”)

d) (”, ‘cd’, ”)

 

Check Answer

Answer: d
The entire string has been passed as the separator hence the first and the last item of the tuple returned are null strings.

 

 

112. What will be the output of the following Python code snippet?

print(‘abef’.partition(‘cd’))

a) (‘abef’)

b) (‘abef’, ‘cd’, ”)

c) (‘abef’, ”, ”)

d) error

Check Answer

 

Answer: c
The separator is not present in the string hence the second and the third elements of the tuple are null strings.

 

113. What will be the output of the following Python code snippet?

print(‘abcdef12’.replace(‘cd’, ’12’))

a) ab12ef12

b) abcdef12

c) ab12efcd

d) none of the mentioned

Check Answer

Answer: a
All occurrences of the first substring are replaced by the second substring.

 

 

114. What will be the output of the following Python code snippet?

print(‘abef’.replace(‘cd’, ’12’))

a) abef

b) 12

c) error

d) none of the mentioned

Check Answer

 

Answer: a
The first substring is not present in the given string and hence nothing is replaced.

 

115. What will be the output of the following Python code snippet?

print(‘abcefd’.replace(‘cd’, ’12’))

a) ab1ef2

b) abcefd

c) ab1efd

d) ab12ed2

Check Answer

Answer: b
The first substring is not present in the given string and hence nothing is replaced.

 

 

116. What will be the output of the following Python code snippet?

print(‘xyyxyyxyxyxxy’.replace(‘xy’, ’12’, 0))

a) xyyxyyxyxyxxy

b) 12y12y1212x12

c) 12yxyyxyxyxxy

d) xyyxyyxyxyx12

Check Answer

 

Answer: a
The first 0 occurrences of the given substring are replaced.

 

117. What will be the output of the following Python code snippet?

print(‘xyyxyyxyxyxxy’.replace(‘xy’, ’12’, 100))

a) xyyxyyxyxyxxy

b) 12y12y1212x12

c) none of the mentioned

d) error

Check Answer

 

Answer: b
The first 100 occurrences of the given substring are replaced.

 

118. What will be the output of the following Python code snippet?

print(‘abcdefcdghcd’.split(‘cd’))

a) [‘ab’, ‘ef’, ‘gh’]

b) [‘ab’, ‘ef’, ‘gh’, ”]

c) (‘ab’, ‘ef’, ‘gh’)

d) (‘ab’, ‘ef’, ‘gh’, ”)

Check Answer

Answer: b
The given string is split and a list of substrings is returned.

 

 

119. What will be the output of the following Python code snippet?

print(‘abcdefcdghcd’.split(‘cd’, 0))

a) [‘abcdefcdghcd’]

b) ‘abcdefcdghcd’

c) error

d) none of the mentioned

Check Answer

 

Answer: a
The given string is split at 0 occurances of the specified substring.

 

120. What will be the output of the following Python code snippet?

print(‘abcdefcdghcd’.split(‘cd’, -1))

a) [‘ab’, ‘ef’, ‘gh’]

b) [‘ab’, ‘ef’, ‘gh’, ”]

c) (‘ab’, ‘ef’, ‘gh’)

d) (‘ab’, ‘ef’, ‘gh’, ”)

Check Answer

Answer: b
Calling the function with a negative value for maxsplit is the same as calling it without any maxsplit specified. The string will be split into as many substring s as possible.

 

Pages  1  2  3  4  5  6  7  8  9  10  11  12  13

 

 

Leave a Comment