Python MCQ and Answers – Strings
This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Variables And Operators”.
1. What is the output of the following code?
print("x"+"yz")
A x
B yz
C xyz
D yzx
2. What is the output of the following code?
str="4/2" print("str")
A 2
B 4/2
C str
D Compilation error
3. Which of the following will raise an error if str = "hello"
?
A print(str[3])
B str[2]="a"
C print(str[0:9])
D Both B and C
4. What is the output of print("hello"[2:])
?
A he
B hel
C lo
D llo
5. What is the output of the following code?
str = "StackHowTo" print(str[5:8])
A StackHow
B HowTo
C How
D StackHowTo
6. What is the output of the following code?
str1 = "StackHowTo" str2 = str1.replace('o','O') print(str2)
A StackHOwTo
B StackHOwTO
C StackHowTo
D StackHowTO
7. Which of the following operators cannot be used when handling strings?
A *
B –
C +
D All of the above
8. What is the output of the following code?
print('Stack' 'HowTo')
A StackHowTo
B Stack HowTo
C Stack
D Compilation error
9. What is the output of the following code?
str ="Hello" str.upper() print(str)
A Hello
B HELLO
C hello
D Compilation error
10. What will return the following code?
str = "blue, green, yellow, red" str.find("green")
A It returns the 1st index position of the 1st occurrence of “green” in str.
B It returns the last index position of the last occurrence of “green” in str.
C It returns the last index position of the 1st occurrence of “green” in str.
D It returns the 1st index position of the 1st occurrence of “green” in str.