Python MCQ – String – 3

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

1. What will be the output of the following Python statement?

>>>chr(ord('A'))

a) A
b) B
c) a
d) Error
View Answer

Answer: a
Explanation: Execute in shell to verify.
advertisement
advertisement

2. What will be the output of the following Python statement?

>>>print(chr(ord('b')+1))

a) a
b) b
c) c
d) A
View Answer

Answer: c
Explanation: Execute in the shell to verify.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. Which of the following statement prints hello\example\test.txt?
a) print(“hello\example\test.txt”)
b) print(“hello\\example\\test.txt”)
c) print(“hello\”example\”test.txt”)
d) print(“hello”\example”\test.txt”)
View Answer

Answer: b
Explanation: \is used to indicate that the next \ is not an escape sequence.
advertisement

4. Suppose s is “\t\tWorld\n”, what is s.strip()?
a) \t\tWorld\n
b) \t\tWorld\n
c) \t\tWORLD\n
d) World
View Answer

Answer: d
Explanation: Execute help(string.strip) to find details.

5. The format function, when applied on a string returns ___________
a) Error
b) int
c) bool
d) str
View Answer

Answer: d
Explanation: Format function returns a string.
advertisement

6. What will be the output of the “hello” +1+2+3?
a) hello123
b) hello
c) Error
d) hello6
View Answer

Answer: c
Explanation: Cannot concatenate str and int objects.

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

>>>print("D", end = ' ')
>>>print("C", end = ' ')
>>>print("B", end = ' ')
>>>print("A", end = ' ')

a) DCBA
b) A, B, C, D
c) D C B A
d) D, C, B, A will be displayed on four lines
View Answer

Answer: c
Explanation: Execute in the shell.

8. What will be the output of the following Python statement?(python 3.xx)

>>>print(format("Welcome", "10s"), end = '#')
>>>print(format(111, "4d"), end = '#')
>>>print(format(924.656, "3.2f"))

a)    Welcome# 111#924.66
b) Welcome#111#924.66
c) Welcome#111#.66
d) Welcome   # 111#924.66
View Answer

Answer: d
Explanation: Execute in the shell to verify.

9. What will be displayed by print(ord(‘b’) – ord(‘a’))?
a) 0
b) 1
c) -1
d) 2
View Answer

Answer: b
Explanation: ASCII value of b is one more than a. Hence the output of this code is 98-97, which is equal to 1.

10. Say s=”hello” what will be the return value of type(s)?
a) int
b) bool
c) str
d) String
View Answer

Answer: c
Explanation: str is used to represent strings in python.

Sanfoundry Global Education & Learning Series – Python.

To practice all technical interview questions on 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.