Python Questions and Answers – Strings – 4
This set of Python Coding Questions & Answers focuses on “Strings”.
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
a) Peter Pan.
b) John Pan.
c) Peter Smith.
d) John Smith.
View Answer
1. What is “Hello”.replace(“l”, “e”)
a) Heeeo
b) Heelo
c) Heleo
d) None
View Answer
a) Heeeo
b) Heelo
c) Heleo
d) None
View Answer
Answer:a
Explanation:Execute in shell to verify.
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[3] b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
View Answer
a) s[3] b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
View Answer
Answer:a, c
Explanation:__getitem(..) can be used to get character at index specified as parameter.
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 (multiple answers allowed) ?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
View Answer
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
View Answer
Answer:a,b
Explanation:Execute in shell to verify.
Explanation:Execute in shell to verify.
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.(multiple answers allowed)
a) obj.__str__()
b) str(obj)
c) print obj
d) __str__(obj)
View Answer
a) obj.__str__()
b) str(obj)
c) print obj
d) __str__(obj)
View Answer
Answer:a,b,c
Explanation:Execute in shell to verify.
Explanation:Execute in shell to verify.
5. To check whether string s1 contains s2, use
a) s1.__contains__(s2)
b) s1 in s2
c) s1.contains(s2)
d) si.in(s2)
View Answer
a) s1.__contains__(s2)
b) s1 in s2
c) s1.contains(s2)
d) si.in(s2)
View Answer
Answer:a,b
Explanation:s1 in s2 works in the same way as calling the special function __contains__ .
Explanation:s1 in s2 works in the same way as calling the special function __contains__ .
advertisements
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.
Explanation:Execute in shell to verify.
7. What is the output of the following code ?
a) True False
b) True True
c) False True
d) False False
View Answer
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.
Explanation:Execute in the shell objects cannot have same id, however in the case of strings its different.
8. What is the output of the following code ?
advertisements
b) John Pan.
c) Peter Smith.
d) John Smith.
View Answer
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
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.
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
a) __345.355
b) ___345.355
c) ____345.355
d) _____345.354
View Answer
Answer:b
Explanation:Execute in the shell to verify.
Explanation:Execute in the shell to verify.
No comments:
Post a Comment