Python MCQs
1. What will be the output of the following Python code?
print('Hello!2@#World'.istitle())
a) True
b) False
c) None
d) error
View Answer
Answer: a
Explanation: It is in the form of a title.
2. What will be the output of the following Python code?
print('1Rn@'.lower())
a) n
b) 1rn@
c) rn
d) r
View Answer
Answer: b
Explanation: Uppercase letters are converted to lowercase. The other characters are left unchanged.
3. What will be the output of the following Python code?
print(''' \tfoo'''.lstrip())
a) \tfoo
b) foo
c) foo
d) none of the mentioned
View Answer
Explanation: All leading whitespace is removed.
4. What will be the output of the following Python code?
print('xyyzxxyxyy'.lstrip('xyy'))
a) error
b) zxxyxyy
c) z
d) zxxy
View Answer
Answer: a
Explanation: All combinations of the characters passed as an argument are removed from the left hand side.
5. What will be the output of the following Python code?
print('xyxxyyzxxy'.lstrip('xyy'))
a) zxxy
b) xyxxyyzxxy
c) xyxzxxy
d) none of the mentioned
View Answer
Answer: a
Explanation: All combinations of the characters passed as an argument are removed from the left hand side.
6. What will be the output of the following Python code?
print('cba'.maketrans('abc', '123'))
a) {97: 49, 98: 50, 99: 51}
b) {65: 49, 66: 50, 67: 51}
c) 321
d) 123
View Answer
Answer: a
Explanation: A translation table is returned by maketrans.
7. What will be the output of the following Python code?
print('a'.maketrans('ABC', '123'))
a) {97: 49, 98: 50, 99: 51}
b) {65: 49, 66: 50, 67: 51}
c) {97: 49}
d) 1
View Answer
Answer: b
Explanation: maketrans() is a static method so it’s behaviour does not depend on the object from which it is being called.
8. What will be the output of the following Python code?
print('abcdef'.partition('cd'))
a) (‘ab’, ‘ef’)
b) (‘abef’)
c) (‘ab’, ‘cd’, ‘ef’)
d) 2
View Answer
Answer: c
Explanation: The string is split into three parts by partition.
9. What will be the output of the following Python code?
print('abcdefcdgh'.partition('cd'))
a) (‘ab’, ‘cd’, ‘ef’, ‘cd’, ‘gh’)
b) (‘ab’, ‘cd’, ‘efcdgh’)
c) (‘abcdef’, ‘cd’, ‘gh’)
d) error
View Answer
Answer: b
Explanation: The string is partitioned at the point where the separator first appears.
10. What will be the output of the following Python code?
print('abcd'.partition('cd'))
a) (‘ab’, ‘cd’, ”)
b) (‘ab’, ‘cd’)
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: The last item is a null string.