Python MCQ – String – 2

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

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

class father:
    def __init__(self, param):
        self.o1 = param
 
class child(father):
    def __init__(self, param):
        self.o2 = param
 
>>>obj = child(22)
>>>print "%d %d" % (obj.o1, obj.o2)

a) None None
b) None 22
c) 22 None
d) Error is generated
View Answer

Answer: d
Explanation: self.o1 was never created.
advertisement
advertisement

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

class tester:
    def __init__(self, id):
        self.id = str(id)
        id="224"
 
>>>temp = tester(12)
>>>print(temp.id)

a) 224
b) Error
c) 12
d) None
View Answer

Answer: c
Explanation: Id in this case will be the attribute of the class.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
>>>example = "snow world"
>>>print("%s" % example[4:7])

a) wo
b) world
c) sn
d) rl
View Answer

Answer: a
Explanation: Execute in the shell and verify.
advertisement

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

>>>example = "snow world"
>>>example[3] = 's'
>>>print example

a) snow
b) snow world
c) Error
d) snos world
View Answer

Answer: c
Explanation: Strings cannot be modified.

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

>>>max("what are you")

a) error
b) u
c) t
d) y
View Answer

Answer: d
Explanation: Max returns the character with the highest ascii value.

6. Given a string example=”hello” what is the output of example.count(‘l’)?
a) 2
b) 1
c) None
d) 0
View Answer

Answer: a
Explanation: l occurs twice in hello.

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

>>>example = "helle"
>>>example.find("e")

a) Error
b) -1
c) 1
d) 0
View Answer

Answer: c
Explanation: Returns lowest index.

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

>>>example = "helle"
>>>example.rfind("e")

a) -1
b) 4
c) 3
d) 1
View Answer

Answer: b
Explanation: Returns highest index.

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

>>>example="helloworld"
>>>example[::-1].startswith("d")

a) dlrowolleh
b) True
c) -1
d) None
View Answer

Answer: b
Explanation: Starts with checks if the given string starts with the parameter that is passed.

10. To concatenate two strings to a third what statements are applicable?
a) s3 = s1 . s2
b) s3 = s1.add(s2)
c) s3 = s1.__add__(s2)
d) s3 = s1 * s2
View Answer

Answer: c
Explanation: __add__ is another method that can be used for concatenation.

Sanfoundry Global Education & Learning Series – Python.

To practice all advanced interview questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.