Search Tutorials


Python Strings MCQ Questions and Answers | JavaInUse

Python Strings MCQ Questions and Answers

Q. What is the output of the following Python code?

    my_string = "Hello, World!"
    print(my_string[::2])
   
A. Hell, Wrd
B. Hello, World!
C. Hell, World
D. Error: Invalid slice notation

Q. What is the output of the following Python code?

    my_string = "Python is awesome"
    print(my_string.replace("Python", "Java"))
    
A. Java is awesome
B. Python is Java
C. Python Java awesome
D. Error: Invalid syntax

Q. What is the output of the following Python code?

    my_string = "Hello"
    print(my_string.upper())
    print(my_string)
    
A. HELLO
Hello
B. HELLO
hello
C. Hello
HELLO
D. Hello
hello

Q. What is the output of the following Python code?

    my_string = "   Python   "
    print(my_string.strip())
    
A. Python
B. Python
C. Python
D. Error: Invalid syntax

Q. What is the output of the following Python code?

    my_string = "Hello, World!"
    print(my_string[3:8])
   
A. ello,
B. lo, W
C. lo, Wo
D. Error: Invalid slice notation

Q. What is the output of the following Python code?

    my_string = "Hello"
    print(my_string.isalpha())
    
A. True
B. False
C. Error: Invalid method

Q. What is the output of the following Python code?

    my_string = "Hello, World!"
    print(my_string.startswith("Hello"))
    
A. True
B. False
C. Error: Invalid method

Q. What is the output of the following Python code?

    my_string = "Hello"
    print(my_string.find("ell"))
    
A. 1
B. 2
C. -1
D. Error: Invalid method

Q. What is the output of the following Python code?

    my_string = "Hello, World!"
    print(my_string.split(","))
    
A. ['Hello', ' World!']
B. ['Hello', 'World']
C. ['Hello, World!']
D. Error: Invalid method

Q. What is the output of the following Python code?

    my_string = "Hello"
    print(my_string.isalnum())
    
A. True
B. False
C. Error: Invalid method





Q. What is the output of the following Python code?

    my_string = "Hello, World!"
    print(my_string.endswith("!"))
    
A. True
B. False
C. Error: Invalid method

Q. What is the output of the following Python code?

    my_string = "Hello"
    print(my_string.title())
    
A. Hello
B. HELLO
C. HeLLo
D. hello

Q. What is the output of the following Python code?

    my_string = "   Python   "
    print(my_string.lstrip())
    
A. Python
B. Python
C. Python
D. Error: Invalid method

Q. What is the output of the following Python code?

    my_string = "Hello, World!"
    print(my_string.count("l"))
    
A. 3
B. 2
C. 1
D. 0

Q. What is the output of the following Python code?

    my_string = "Hello"
    print(my_string.rjust(10, "-"))
    
A. Hello-----
B. -----Hello
C. Hello--
D. Error: Invalid method

Q. What will be the output of the following code snippet?

my_string = "Hello, World!"
print(my_string.upper())
A.
HELLO, WORLD!
B.
hello, world!
C.
Error: Invalid string method
D.
None of the above

Q. How can you extract the last 3 characters from the string "Python Programming"?

A.
my_string = "Python Programming"
last_3_chars = my_string[-3:]
print(last_3_chars)
B.
my_string = "Python Programming"
last_3_chars = my_string[len(my_string)-3:]
print(last_3_chars)
C.
my_string = "Python Programming"
last_3_chars = my_string[len(my_string)-3:-1]
print(last_3_chars)
D.
my_string = "Python Programming"
last_3_chars = my_string[len(my_string)-4:-1]
print(last_3_chars)

Q. What will be the output of the following code snippet?

my_string = "Hello, World!"
print(my_string.replace("Hello", "Goodbye"))
A.
Goodbye, World!
B.
Hello, Goodbye!
C.
Hello, World! Goodbye
D.
Error: Invalid syntax

Q. How can you check if a string is a palindrome?

A.
def is_palindrome(my_string):
    return my_string == my_string[::-1]

my_string = "racecar"
print(is_palindrome(my_string))
B.
def is_palindrome(my_string):
    return my_string == my_string[::-1]

my_string = "racecar"
print(my_string == my_string[::-1])
C.
def is_palindrome(my_string):
    return my_string == my_reverse(my_string)

def my_reverse(my_string):
    return my_string[::-1]

my_string = "racecar"
print(is_palindrome(my_string))
D.
def is_palindrome(my_string):
    return my_string == my_string[len(my_string)-1:-1]

my_string = "racecar"
print(is_palindrome(my_string))

Q. What will be the output of the following code snippet?

my_string = "Hello, World!"
print(my_string[2:7])
A.
llo, W
B.
ello,
C.
Hello, World!
D.
Error: Invalid syntax

Q. How can you count the number of occurrences of a character in a string?

A.
my_string = "Hello, World!"
char_to_count = "l"
count = my_string.count(char_to_count)
print(count)
B.
my_string = "Hello, World!"
char_to_count = "l"
count = 0
for char in my_string:
    if char == char_to_count:
        count += 1
print(count)
C.
my_string = "Hello, World!"
char_to_count = "l"
count = len(my_string.split(char_to_count)) - 1
print(count)
D.
my_string = "Hello, World!"
char_to_count = "l"
count = 0
for i in range(len(my_string)):
    if my_string[i:i+1] == char_to_count:
        count += 1
print(count)

Q. What will be the output of the following code snippet?

my_string = "Hello, World!"
print(my_string.find("World"))
A.
7
B.
-1
C.
Error: Invalid method
D.
Hello

Q. How can you remove leading and trailing spaces from a string?

A.
my_string = "   Hello, World!   "
trimmed_string = my_string.strip()
print(trimmed_string)
B.
my_string = "   Hello, World!   "
trimmed_string = my_string.rstrip()
print(trimmed_string)
C.
my_string = "   Hello, World!   "
trimmed_string = my_string.lstrip()
print(trimmed_string)
D.
my_string = "   Hello, World!   "
trimmed_string = my_string.replace(" ", "")
print(trimmed_string)

Q. What will be the output of the following code snippet?

my_string = "Hello, World!"
print(my_string.startswith("Hello"))
A.
True
B.
False
C.
hello
D.
Error: Invalid method

Q. How can you join a list of strings into a single string with a delimiter?

A.
my_list = ["Hello", "World", "!!!"]
joined_string = ", ".join(my_list)
print(joined_string)
B.
my_list = ["Hello", "World", "!!!"]
joined_string = " ".join(my_list)
print(joined_string)
C.
my_list = ["Hello", "World", "!!!"]
joined_string = "".join(my_list)
print(joined_string)
D.
my_list = ["Hello", "World", "!!!"]
joined_string = join(my_list, ", ")
print(joined_string)