Test Series - python

Test Number 26/108

Q: What is “Hello”.replace(“l”, “e”)?
A. Heeeo
B. Heelo
C. Heleo
D. None
Solution: Execute in shell to verify.
Q: 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)
Solution: __getitem(..) can be used to get character at index specified as parameter.
Q: To return the length of string s what command do we execute?
A. s.__len__()
B. len(s)
C. size(s)
D. s.size()
Solution: Execute in shell to verify.
Q: 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
Solution: Execute in shell to verify.
Q: 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)
Solution: s2 in s1 works in the same way as calling the special function __contains__ .
Q: 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)
Solution: Execute in shell to verify.
Q: What will be the output of the following Python code?

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
Solution: Execute in the shell objects cannot have same id, however in the case of strings its different.
Q: 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”))
Solution: Execute in shell to verify.
Q: 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
Solution: Execute in the shell to verify.

You Have Score    /9