Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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

print("abc DEF".capitalize())

a) abc def
b) ABC DEF
c) Abc def
d) Abc Def

View Answer

Answer: c
Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.




2 - Question

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

print("abc. DEF".capitalize())

a) abc. def
b) ABC. DEF
c) Abc. def
d) Abc. Def

View Answer

Answer: c
Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.




3 - Question

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

print("abcdef".center())

a) cd
b) abcdef
c) error
d) none of the mentioned

View Answer

Answer: c
Explanation: The function center() takes at least one parameter.




4 - Question

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

print("abcdef".center(0))

a) cd
b) abcdef
c) error
d) none of the mentioned

View Answer

Answer: b
Explanation: The entire string is printed when the argument passed to center() is less than the length of the string.




5 - Question

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

print('*', "abcdef".center(7), '*')

a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*

View Answer

Answer: b
Explanation: Padding is done towards the left-hand-side first when the final string is of odd length. Extra spaces are present since we haven’t overridden the value of sep.




6 - Question

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

print('*', "abcdef".center(7), '*', sep='')

a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*

View Answer
Answer: d
Explanation: Padding is done towards the left-hand-side first when the final string is of odd length.



7 - Question

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

print('*', "abcde".center(6), '*', sep='')

a) * abcde *
b) * abcde *
c) *abcde *
d) * abcde*

View Answer

Answer: c
Explanation: Padding is done towards the right-hand-side first when the final string is of even length.




8 - Question

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

print("abcdef".center(7, 1))

a) 1abcdef
b) abcdef1
c) abcdef
d) error

View Answer

Answer: d
Explanation: TypeError, the fill character must be a character, not an int.




9 - Question

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

print("abcdef".center(7, '1'))

a) 1abcdef
b) abcdef1
c) abcdef
d) error

View Answer

Answer: a
Explanation: The character ‘1’ is used for padding instead of a space.




10 - Question

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

print("abcdef".center(10, '12'))

a) 12abcdef12
b) abcdef1212
c) 1212abcdef
d) error

View Answer
Answer: d
Explanation: The fill character must be exactly one character long.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners