239). What is “Hello”.replace(“l”, “e”)?
[A]Heeeo
[B]Heelo
[C]Heleo
[D]None
Show Answer
Correct Answer: Heeeo
Notes:
Answer: a
Explanation: Execute in shell to verify.
240). 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)
Show Answer
Correct Answer: s.__getitem__(3)
Notes:
Answer: c
Explanation: __getitem(..) can be used to get character at index specified as parameter.
241). To return the length of string s what command do we execute?
[A]s.__len__()
[B]len(s)
[C]size(s)
[D]s.size()
Show Answer
Correct Answer: s.__len__()
Notes:
Answer: a
Explanation: Execute in shell to verify.
242). 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
Show Answer
Correct Answer: all of the mentioned
Notes:
Answer: d
Explanation: Execute in shell to verify.
243). 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)
Show Answer
Correct Answer: s1.__contains__(s2)
Notes:
Answer: a
Explanation: s2 in s1 works in the same way as calling the special function __contains__ .
244). 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)
Show Answer
Correct Answer: i.__add__(j)
Notes:
Answer: b
Explanation: Execute in shell to verify.
245). 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
Show Answer
Correct Answer: False True
Notes:
Answer: c
Explanation: Execute in the shell objects cannot have same id, however in the case of strings its different.
246). 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
Show Answer
Correct Answer: John Pan
Notes:
Answer: b
Explanation: Execute in the shell to verify.
247). What function do you use to read a string?
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]input(“Enter a string”)
[B]eval(input(“Enter a string”))
[C]enter(“Enter a string”)
[D]eval(enter(“Enter a string”))
Show Answer
Correct Answer: input(“Enter a string”)
Notes:
Answer: a
Explanation: Execute in shell to verify.
248). Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space).
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]__345.355
[B]___345.355
[C]____345.355
[D]_____345.354
Show Answer
Correct Answer: ___345.355
Notes:
Answer: b
Explanation: Execute in the shell to verify.