Python MCQ – String – 6

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Strings – 6”.

1. 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
View Answer

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

2. 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
View Answer

Answer: a
Explanation: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 1.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
print("xyyzxyzxzxyy".count('yy', 2))

a) 2
b) 0
c) 1
d) none of the mentioned
View Answer

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

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

print("xyyzxyzxzxyy".count('xyy', 0, 100))

a) 2
b) 0
c) 1
d) error
View Answer

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

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

print("xyyzxyzxzxyy".count('xyy', 2, 11))

a) 2
b) 0
c) 1
d) error
View Answer

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

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

print("xyyzxyzxzxyy".count('xyy', -10, -1))

a) 2
b) 0
c) 1
d) error
View Answer

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

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

print('abc'.encode())

a) abc
b) ‘abc’
c) b’abc’
d) h’abc’
View Answer

Answer: c
Explanation: A bytes object is returned by encode.

8. What is the default value of encoding in encode()?
a) ascii
b) qwerty
c) utf-8
d) utf-16
View Answer

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

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

print("xyyzxyzxzxyy".endswith("xyy"))

a) 1
b) True
c) 3
d) 2
View Answer

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

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

print("xyyzxyzxzxyy".endswith("xyy", 0, 2))

a) 0
b) 1
c) True
d) False
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.