Python MCQs on String: SET 6

51. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.count(‘yy’))

a) 2

b) 0

c) error

d) none of the mentioned

Check Answer
Answer: a
Counts the number of times the substring ‘yy’ is present in the given string.

 

 

52. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.count(‘yy’, 1))

a) 2

b) 0

c) 1

d) none of the mentioned

Check Answer

Answer: a
Counts the number of times the substring ‘yy’ is present in the given string, starting from position 1.

 

 

53. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.count(‘yy’, 2))

a) 2

b) 0

c) 1

d) none of the mentioned

Check Answer

Answer: c
Counts the number of times the substring ‘yy’ is present in the given string, starting from position 2.

 

 

54. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.count(‘xyy’, 0, 100))

a) 2

b) 0

c) 1

d) error

Check Answer

Answer: a
An error will not occur if the end value is greater than the length of the string itself.

 

 

55. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.count(‘xyy’, 2, 11))

a) 2

b) 0

c) 1

d) error

Check Answer

Answer: b
Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.

 

 

56. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.count(‘xyy’, -10, -1))

a) 2

b) 0

c) 1

d) error

Check Answer
Answer: b
Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.

 

 

57. What will be the output of the following Python code?

print(‘abc’.encode())

a) abc

b) ‘abc’

c) b’abc’

d) h’abc’

Check Answer

Answer: c
A bytes object is returned by encode.

 

 

58. What is the default value of encoding in encode()?

a) ascii

b) qwerty

c) utf-8

d) utf-16

Check Answer

Answer: c
The default value of encoding is utf-8.

 

 

59. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.endswith(“xyy”))

a) 1

b) True

c) 3

d) 2

Check Answer

Answer: b
The function returns True if the given string ends with the specified substring.

 

 

60. What will be the output of the following Python code?

print(“xyyzxyzxzxyy”.endswith(“xyy”, 0, 2))

a) 0

b) 1

c) True

d) False

Check Answer

Answer: d
The function returns False if the given string does not end with the specified substring.

 

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

 

 

 

Leave a Comment