Python MCQ – String – 4

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

1. What is “Hello”.replace(“l”, “e”)?
a) Heeeo
b) Heelo
c) Heleo
d) None
View Answer

Answer: a
Explanation: Execute in shell to verify.

2. To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed)?
a) s[]
b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
View Answer

Answer: c
Explanation: __getitem(..) can be used to get character at index specified as parameter.

3. To return the length of string s what command do we execute?
a) len(s) OR s.__len__()
b) len(s) only
c) size(s)
d) s.size()
View Answer

Answer: a
Explanation: len(s) is a built-in function in Python to get the length of the string s, whereas s.__len__() calls the __len__ method of the string object s. It’s less common but functionally equivalent to len(s). So, we can use either of these.
advertisement
advertisement

4. If a class defines the __str__(self) method, for an object obj for the class, you can use which command to invoke the __str__ method.
a) obj.__str__()
b) str(obj)
c) print obj
d) all of the mentioned
View Answer

Answer: d
Explanation: Execute in shell to verify.

5. To check whether string s1 contains another string s2, use ________
a) s1.__contains__(s2)
b) s2 in s1
c) s1.contains(s2)
d) si.in(s2)
View Answer

Answer: a
Explanation: s2 in s1 works in the same way as calling the special function __contains__ .
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Suppose i is 5 and j is 4, i + j is same as ________
a) i.__add(j)
b) i.__add__(j)
c) i.__Add(j)
d) i.__ADD(j)
View Answer

Answer: b
Explanation: Execute in shell to verify.

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

advertisement
class Count:
    def __init__(self, count = 0):
       self.__count = count
 
c1 = Count(2)
c2 = Count(2)
print(id(c1) == id(c2), end = " ")
 
s1 = "Good"
s2 = "Good"
print(id(s1) == id(s2))

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

Answer: c
Explanation: Execute in the shell objects cannot have same id, however in the case of strings its different.
advertisement

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

class Name:
    def __init__(self, firstName, mi, lastName):
        self.firstName = firstName
        self.mi = mi
        self.lastName = lastName
 
firstName = "John"
name = Name(firstName, 'F', "Smith")
firstName = "Peter"
name.lastName = "Pan"
print(name.firstName, name.lastName)

a) Peter Pan
b) John Pan
c) Peter Smith
d) John Smith
View Answer

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

9. What function do you use to read a string?
a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))
View Answer

Answer: a
Explanation: Execute in shell to verify.

10. Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space).
a) __345.355
b) ___345.355
c) ____345.355
d) _____345.354
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all coding 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.