MCQ

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

C
The + operator is used to concatenate two or more strings in Python.
 

2. What is the output of the following code?
str="4/2"
print("str")

A 2

B 4/2

C str

D Compilation error

C
In the above code, “str” is written within double quotes so it will print str as output.
 

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

B
Strings are immutable. So, they cannot be modified.
 

4. What is the output of print("hello"[2:])?

A he

B hel

C lo

D llo

D
In the above code we have applied the Slice operation on the string.
 

5. What is the output of the following code?
str = "StackHowTo"
print(str[5:8])

A StackHow

B HowTo

C How

D StackHowTo

C
In the above code we have applied the Slice operation on the string.
 

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

B
In the above code replace() function is used to replace all the existing “o” by “O” in the given string.
 

7. Which of the following operators cannot be used when handling strings?

A *

B

C +

D All of the above

B
+ operator is used to concatenate and * operator is used to multiply strings.
 

8. What is the output of the following code?
print('Stack' 'HowTo')

A StackHowTo

B Stack HowTo

C Stack

D Compilation error

A
String disconnected by whitespace are permited. They are concatenated.
 

9. What is the output of the following code?
str ="Hello"

str.upper()

print(str)

A Hello

B HELLO

C hello

D Compilation error

A
str.upper() returns the uppercase of str. But, it doesn’t change the string. So, it will output the original string.
 

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.

A
It returns the 1st index position of the 1st occurrence of “green” in str.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *