Strings in Python
MCQ on Strings
Q1. Following is an example of ___________________
v = "cbsecs.in"
-a. List
-b. String
-c. Dictionary
-d. None of the above
Answer
#Ans. b. String
Q2. Python considered the character enclosed in triple quotes as String.(T/F)
-a. True
-b. False
Answer
#Ans. a. True
Q3. Write the output of the following.
a = "Blog"
a ='a'
print(a)
-a. Bloga
-b. aBlog
-c. Blog
-d. a
Answer
#Ans. d. a
Q4. Which function in the following statement will execute last?
print("amit".lower().upper())
-a. print( )
-b. lower( )
-c. upper( )
-d. None of the above
Answer
#Ans. a. print( )
Q5. Write the output of the following code :
a= "Welcome to \"my\" blog"
print(a)
-a. Welcome to "my" blog
-b. Welcome to \"my\" blog
-c. Error
-d. None of the above
Answer
#Ans. a. Welcome to "my" blog
Q6. Write the output of the following:
print('aisabisacisadisae'.split('isa',3))
-a. [‘a’, ‘b’, ‘c’, ‘disae’]
-b. [‘a’, ‘b’, ‘cisadisae’]
-c. [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
-d. None of the above
Answer
#Ans. a. [‘a’, ‘b’, ‘c’, ‘disae’]
Q7. Write the output of the following code:
str = "Welcome"
str[2] = 'a'
print(str)
-a. Weacome
-b. Error
-c. aWelcome
-d. Welcomea
Answer
#Ans. b. Error
Q8. Write the output of the following code :
str = "Welcome"
l=len(str)
print(l)
-a. 6
-b. 7
-c. 8
-d. Error
Answer
#Ans. b. 7
Q9. What we call the following:
\n
\t
-a. Escape Sequence
-b. Special Character
-c. Don’t have any common term
-d. Keyword
Answer
#Ans. a. Escape Sequence
Q10. Write the output of the following:
print('aisabisacisadisae'.split('isa'))
-a. [‘a’, ‘b’, ‘c’, ‘disae’]
-b. [‘a’, ‘b’, ‘cisadisae’]
-c. [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
-d. None of the above
Answer
#Ans. c. [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Q11. What is the index value of ‘i’ in string "Learning"
-a. 5
-b. 3
-c. 6
-d. 7
Answer
#Ans. a. 5
Q12. Index value in String should be of type ________________
-a. Float
-b. Integer
-c. String
-d. Boolean
Answer
#Ans. b. Integer
Q13. What type of error is returned by the following:
str = "Learning"
print(str[10])
-a. Error in Index
-b. Index out of range in string
-c. IndexError
-d. None of the above
Answer
#Ans. c. IndexError
Q14. print("aNd&*".swapcase()) will display __________
-a. AnD*&
-b. AnD&*
-c. aNd&*
-d. Error
Answer
#Ans. b. AnD&*
Q15. String traversal can be done using ‘for’ loop only.(T/F)
-a. True
-b. False
Answer
#Ans. b. False
Q16. Which of the following statement is correct in accessing each element of string?
-a)
a="Learning"
while(i):
print(i)
-b)
a="Learning"
for i in a:
print(i)
-a. a only
-b. b only
-c. both a and b
-d. None of the above
Answer
#Ans. b. b only
Q17. Name the function which is used to find length of string.
-a. length( )
-b. len( )
-c. strlen( )
-d. slen( )
Answer
#Ans. b. len( )
Q18. Write the output of the following code :
for i in "STR":
print(i)
-a. S
TR
-b. STR
-c. S T R
-d. S
T
R
Answer
#Ans. d.
Q19. Write the output of the following code :
a="Learn"
while(a):
print(a)
-a.
L
e
-a
r
n
-b. Error
-c. Infinite Loop
-d. Learn
Answer
#Ans. c. Infinite Loop
Q20. Which operator is used with integers as well as with strings?
-a. /
-b. //
-c. **
-d. *
Answer
#Ans. d. *
Q21. Write the output of the following:
2 + '3'
-a. 2 3
-b. 23
-c. SyntaxError
-d. TypeError
Answer
#Ans. d. TypeError
Q22. Which operator is used for string concatenation?
-a. *
-b. //
-c. +
-d. –
Answer
#Ans. c. +
Q23. Write the output of the following code :
for i in (1,2,3):
print("@" * i)
-a. Error
-b. @@@@@@
-c. @
@@
@@@
-d. @ @ @ @ @ @
Answer
#Ans. c.
Q24. How many operators are used in the following statement?
7 + 4 * 8 // 2 ** 2 - 6 / 1
-a. 5
-b. 6
-c. 7
-d. 8
Answer
#Ans. b. 6
Q25. Write the output of the following code :
s="blog"
for i in range(-1,-len(s),-1):
print(s[i],end="$")
-a. g$o$l$b$
-b. g$o$l$
-c. Error
-d. None of the above
Answer
#Ans. b. g$o$l$
Q26. Write the output of the following code:
s= "str"
s1 = "string"
print(s1 in s)
-a. True
-b. False
-c. Error
-d. None of the above
Answer
#Ans. b. False
Q27. Write the output of the following code:
print("Amita" > "amit")
-a. True
-b. False
-c. Error
-d. None of the above
Answer
#Ans. b. False
Q28. What is the ASCII value of "A"?
-a. 97
-b. 66
-c. 65
-d. 96
Answer
#Ans. c. 65
Q29. Write the output of the following code :
print("Str"[1:2])
-a. t
-b. No Output
-c. Error
-d. None of the above
Answer
#Ans. a. t
Q30. Write the output of the following code :
for i in range(65,70):
print(chr(i))
-a. Error
-b. TypeError
-c. A
B
C
D
E
-d. ABCDE
Answer
#Ans. c.
Q31. Write the output of the following:
print("A#B#C#D#E".split("#",2))
-a. [‘A’, ‘B’, ‘C#D#E’]
-b. [‘A#’, ‘B#’, ‘C#D#E’]
-c. [‘A’, ‘B’, ‘C’ , ‘D’ , ‘E’]
-d. Error
Answer
#Ans. a. [‘A’, ‘B’, ‘C#D#E’]
Q32. Write the output of the following:
print("A#B#C#D#E".replace("#","?"))
-a. A#B#C#D#E
-b. A?B?C?D?E
-c. Can not replace as Strings are immutable
-d. Error
Answer
#Ans. b. A?B?C?D?E
Q33. Write the output of the following:
print("I love my Country".find("o",4))
-a. 3
-b. 12
-c. 11
-d. 3,11
Answer
#Ans. c. 11
Q34. Write the output of the following.
print(‘#’.join("12345"))
-a. #12345
-b. #12345#
-c. 1#2#3#4#5
-d. 1#2#3#4#5#
Answer
#Ans. c. 1#2#3#4#5
Q35. Which of the following is not a String built in functions?
-a. isupper( )
-b. lower( )
-c. partition( )
-d. swapcases( )
Answer
#Ans. d. swapcases( )
Q36. Write the output of the following:
print('hash-tag'.title())
-a. HashTag
-b. Hash-tag
-c. Hash-Tag
-d. Error
Answer
#Ans. c. Hash-Tag
Q37. Write the output of the following:
print(len("\\\\\\///"))
-a. 9
-b. Error
-c. 3
-d. 6
Answer
#Ans. d. 6
Q38. Which of the following function returns the ASCII/Unicode value character?
-a. asc( )
-b. ord( )
-c. asci( )
-d. ascii( )
Answer
#Ans. b. ord()
Q39. Which of the following statements will also return the same result as given statement?
print("Computer" + "Computer")
-a. print("Computer" , " ", "Computer")
-b. print("Computer","Computer")
-c. print("Computer" **2)
-d. print("Computer" * 2)
Answer
#Ans. d. print("Computer" * 2)
Q40. print(‘Welcome TO My Blog’.istitle()) will display ________________
-a. True
-b. False
-c. Error
-d. None of the above
Answer
#Ans. b. False
Q41. Which of the following is a mapping data type?
-a. String
-b. List
-c. Tuple
-d. Dictionary
Answer
#Ans. d. Dictionary
Q42. A ___________ can be created by enclosing one or more characters in single, double or triple quote.
-a. String
-b. List
-c. Tuple
-d. Dictionary
Answer
#Ans. a. String
Q43. Characters in a string can be _________ enclosed in quotes.
-a. Digits
-b. white space
-c. letter
-d. All of the above
Answer
#Ans. d. All of the above
Q44. Each individual character in a string can be accessed using a technique called ____________
-a. Indexing
-b. Replication
-c. Concatenation
-d. None of the above
Answer
#Ans. a. Indexing
Q45. If we give index value out of the range then we get a/an ___
-a. ValueError
-b. SyntaxError
-c. IndexError
-d. Error
Answer
#Ans. c. IndexError
Q46. The index of the first character ____________ on the string is 0
-a. from left
-b. from right
-c. from middle
-d. none of the above
Answer
#Ans. a. from left
Q47. What type of error is returned by statement :
>>> str[1.5]
-a. SyntaxError
-b. ValueError
-c. IndexError
-d. TypeError
Answer
#Ans. d. TypeError
Q48. Write the output of the following.
str = "python"
print(str[2+1])
-a. t
-b. h
-c. th
-d. Error
Answer
#Ans. b. h
Q49. Content of string cannot be changed, it means that string is:
-a. mutable
-b. immutable
-c. reversible
-d. none of the above
Answer
#Ans. b. immutable
Q50. What type of error is returned by following code:
>>> str1 = "Hello World!"
>>> str1[1] = ‘a’
-a. TypeError
-b. SyntaxError
-c. ValueError
-d. NoError
Answer
#Ans. a. TypeError
Q51. Python allows _____________ operations on string data type.
-a. Concatenation
-b. Membership
-c. Slicing
-d. All of the above
Answer
#Ans. d. All of the above
Q52. Write the output of the following.
>>> str1 = ‘Hello World!’
>>> ‘W’ in str1
-a. True
-b. False
-c. No
-d. None of the above
Answer
#Ans. a. True
Q53. _____________ method is used to access some part of a string or substring.
-a. Slicer
-b. Slicing
-c. Membership
-d. None of the above
Answer
#Ans. b. Slicing
Q54. str1[n:m] returns all the characters starting from str1[n] till str1[m-1]. (T/F)
-a. True
-b. False
Answer
#Ans. a. True
Q55. str[5 : 11] will return ___________ characters.
-a. 5
-b. 6
-c. 11
-d. None of the above
Answer
#Ans. b. 6
Q56. str[11 : 5] will return ____________
-a. empty string
-b. complete string
-c. string of six characters
-d. None of the above
Answer
#Ans. a. empty string
Q57. str[ : ] will return ______________
-a. empty string
-b. Complete String
-c. Partial String
-d. Error
Answer
#Ans. b. Complete String
Q58. Slice operation can also take a third parameter that specifies the ________
-a. Starting index
-b. Ending index
-c. Step Size
-d. None of the above
Answer
#Ans. c. Step Size
Q59. Which of the following is correct slicing operation to extract every kth character from the string str1 starting from n and ending at m-1.
-a. str1[: : k]
-b. str1[n : m+1 : k]
-c. str1[n : m : k ]
-d. str1[m : n : k ]
Answer
#Ans. c. str1[n : m : k ]
Q60. Which of the following will return reverse string str1?
-a. str1[ : : -1]
-b. str1[: : 1]
-c. str1[: -1 : -1 ]
-d. None of the above
Answer
#Ans. a. str1[ : : -1]
Q61 We can access each character of a string or traverse a string using _____________
-a. for loop only
-b. while loop only
-c. both of the above
-d. conditional statement
Answer
#Ans. c. both of the above
Q62. Which of the following function returns the string with first letter of every word in the string in uppercase and rest in lowercase?
-a. swapcase( )
-b. upper( )
-c. title( )
-d. capitalize( )
Answer
#Ans. c. title( )
Q63. Write the output of the following:
s = " Hello"
print(s.find(‘a’))
-a. 0
-b. -1
-c. 1
-d. Error
Answer
#Ans. b. -1
Q64. What index value is returned by the following code?
s = "Hello"
print(s.find(‘l’))
-a. 2
-b. 3
-c. 4
-d. -2
Answer
#Ans. a. 2
Q65. Following code will return ?
s = "Hello"
print(s.count(‘l’,2,5))
-a. 2
-b. 3
-c. 1
-d. none of the above
Answer
#Ans. a. 2
Q66. Name a function which is same as find() but raises an exception if the substring is not present in the given string.
-a. index( )
-b. endswith( )
-c. startswith( )
-d. count( )
Answer
#Ans. a. index( )
Q67. Write the output of the following.
>>> str1 = ‘Hello World! Hello
>>> str1.index(‘Hee’)
-a. 0
-b. -1
-c. Error
-d. None of the above
Answer
#Ans. c. Error
Q68. Write the output of the following.
>>> str1 = ‘Hello World!’
>>> str1.endswith(‘World!’)
-a. True
-b. False
-c. Yes
-d. Error
Answer
#Ans. a. True
Q69. Write the output of the following.
s = "hello 123"
print(s.islower())
-a. False
-b. True
-c. Error
-d. None of the above
Answer
#Ans. b. True
Q70. Write the output of the following:
>>> str1 = ‘HelloWorld!!’
>>> str1.isalnum()
-a. False
-b. True
-c. Error
-d. None of the above
Answer
#Ans. a. False
Q71. Write the output of the following:
>>> str1 = 'hello WORLD!'
>>> str1.title()
-a. HELLO world
-b. Hello World
-c. Hello world
-d. HeLlO WoRlD
Answer
#Ans. b. Hello World
Q72. Write the output of the following:
>>> str1 = 'Hello World! Hello Hello'
>>> str1.count('Hello',12,25)
-a. 1
-b. 2
-c. 3
-d. 4
Answer
#Ans. b. 2
Q73. Write the output of the following :
>>> str1 = 'Hello World! Hello Hello'
>>> str1.count('Hello')
-a. 1
-b. 2
-c. 3
-d. 4
Answer
#Ans. c. 3
Q74. Write the output of the following :
print("Absbcbcgdbc".count("b",4))
-a. 1
-b. 2
-c. 3
-d. 4
Answer
#Ans. b. 2
Q75. Write the output of the following :
print("Absbcbcgdbc".find("b",5))
-a. 1
-b. 2
-c. 5
-d. 6
Answer
#Ans. c. 5
Q76. Which of the following function returns the index value of first occurrence of substring occurring in the given string.
-a. index( )
-b. find( )
-c. Both of the above
-d. None of the above
Answer
#Ans. c. Both of the above
Q77. find( ) function returns ___________ if the substring is not present in the given string
-a. 0
-b. 1
-c. -1
-d. Error
Answer
#Ans. c. -1
Q78. index( ) function returns _______________ if the substring is not present in the given string
-a. 0
-b. 1
-c. -1
-d. Error
Answer
#Ans. d. Error
Q79. >>> "Hey!".endswith(‘!’) will return _____________
-a. True
-b. False
-c. Error
-d. None of the above
Answer
#Ans. a. True
Q80. Which of the function returns Boolean value?
-a. find( )
-b. index( )
-c. endwith( )
-d. endswith( )
Answer
#Ans. d. endswith( )
Q81. Write the output of the following:
>>> str1 = 'String MCQ'
>>> str1.startswith('Str')
-a. True
-b. False
-c. Yes
-d. No
Answer
#Ans. a. True
Q82. Write the output of the following:
print("Welcome-Python".isalnum())
-a. True
-b. False
-c. Yes
-d. No
Answer
#Ans. b. False
Q83. Write the output of the following:
print(str("WelcomePython".isalnum()).upper())
-a. TRUE
-b. True
-c. FALSE
-d. Error
Answer
#Ans. a. TRUE
Q84. Write the output of the following:
>>> str1 = 'hello ??'
>>> str1.islower( )
-a. No
-b. True
-c. False
-d. Error
Answer
#Ans. b. True
Q85. Which of the following function returns True if the string is non-empty and has all uppercase alphabets.
-a. islower( )
-b. isupper( )
-c. Islower( )
-d. istitle
Answer
#Ans. b. isupper( )
Q86. Write the output of the following:
>>> str1 = 'Hello World!'
>>> str1.replace('o' , '*')
-a. ‘Hello World!’
-b. ‘Hell* W*rld!’
-c. ‘Hello W*rld!’
-d. Error
Answer
#Ans. b. ‘Hell* W*rld!’
Q87. Write the output of the following:
>>> str1 = 'India is a Great is Country'
>>> str1.partition('is')
-a. (‘India ‘, ‘is’, ‘ a Great is Country’)
-b. (‘India ‘, ‘is’, ‘a Great’, ‘is’, ‘Country’)
-c. (‘India ‘, ‘is’, ‘ a Great Country’)
-d. Error
Answer
#Ans. a. (‘India ‘, ‘is’, ‘ a Great is Country’)
Q88. Write the output of the following:
str = "Amit is a is a"
s = str.partition('is')
print(s[1])
-a. is a
-b. a
-c. is
-d. i
Answer
#Ans. c. is
Q89. partition( ) function of string in python return the result in the form of ________________
-a. String
-b. List
-c. Tuple
-d. Dictionary
Answer
#Ans. c. Tuple
Q90. partition() function divides the main string into ____________ substring.
-a. 1
-b. 2
-c. 3
-d. 4
Answer
#Ans. c. 3
Q91. split( ) function returns the _______________of words delimited by the specified substring.
-a. List
-b. Tuple
-c. Dictionary
-d. None of the above
Answer
#Ans. a. List
Q92. If no delimiter is given in split( ) function then words are separated by __________
-a. space
-b. colon
-c. semi colon
-d. None of the above
Answer
#Ans. a. space
Q93. Write the output of the following:
"python".join("@")
-a. ‘p@y@t@h@o@n’
-b. ‘p@y@t@h@o@n@’
-c. ‘@p@y@t@h@o@n@’
-d. ‘@’
Answer
#Ans. d. ‘@’
Q94. Write the output of the following:
"@".join("python")
-a. ‘p@y@t@h@o@n’
-b. ‘p@y@t@h@o@n@’
-c. ‘@p@y@t@h@o@n@’
-d. ‘@’
Answer
#Ans. a. ‘p@y@t@h@o@n’
Q95. Write the output of the following:
>>> len(" python".lstrip()) #2 spaces are given before "python"
-a. 5
-b. 6
-c. 7
-d. 8
Answer
#Ans. b. 6
Q96. Which of the following function removes only leading spaces from the string?
-a. strip( )
-b. lstrip( )
-c. rstrip( )
-d. Lstrip( )
Answer
#Ans. b. lstrip( )
Q97. Which of the following function can take maximum three arguments/parameters?
-a. find( )
-b. index( )
-c. count( )
-d. All of the above
Answer
#Ans. d. All of the above
Q98. Which of the following function return the integer value?
-a. lower( )
-b. upper( )
-c. islower( )
-d. find( )
Answer
#Ans. d. find( )
Q99. Which of the following is not an inbuilt function of string?
-a. length( )
-b. find( )
-c. endswith( )
-d. split( )
Answer
#Ans. a. length( )
Q100. Which function in the following statement will execute first?
print("amit".lower().upper())
-a. print( )
-b. lower( )
-c. upper( )
-d. None of the above
Answer
#Ans. b. lower( )
Answer
#Ans. b. String
Answer
#Ans. a. True
Answer
#Ans. d. a
Answer
#Ans. a. print( )
Answer
#Ans. a. Welcome to "my" blog
Answer
#Ans. a. [‘a’, ‘b’, ‘c’, ‘disae’]
Answer
#Ans. b. Error
Answer
#Ans. b. 7
Answer
#Ans. a. Escape Sequence
Answer
#Ans. c. [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Answer
#Ans. a. 5
Answer
#Ans. b. Integer
Answer
#Ans. c. IndexError
Answer
#Ans. b. AnD&*
Answer
#Ans. b. False
Answer
#Ans. b. b only
Answer
#Ans. b. len( )
Answer
#Ans. d.
Answer
#Ans. c. Infinite Loop
Answer
#Ans. d. *
Answer
#Ans. d. TypeError
Answer
#Ans. c. +
Answer
#Ans. c.
Answer
#Ans. b. 6
Answer
#Ans. b. g$o$l$
Answer
#Ans. b. False
Answer
#Ans. b. False
Answer
#Ans. c. 65
Answer
#Ans. a. t
Answer
#Ans. c.
Answer
#Ans. a. [‘A’, ‘B’, ‘C#D#E’]
Answer
#Ans. b. A?B?C?D?E
Answer
#Ans. c. 11
Answer
#Ans. c. 1#2#3#4#5
Answer
#Ans. d. swapcases( )
Answer
#Ans. c. Hash-Tag
Answer
#Ans. d. 6
Answer
#Ans. b. ord()
Answer
#Ans. d. print("Computer" * 2)
Answer
#Ans. b. False
Answer
#Ans. d. Dictionary
Answer
#Ans. a. String
Answer
#Ans. d. All of the above
Answer
#Ans. a. Indexing
Answer
#Ans. c. IndexError
Answer
#Ans. a. from left
Answer
#Ans. d. TypeError
Answer
#Ans. b. h
Answer
#Ans. b. immutable
Answer
#Ans. a. TypeError
Answer
#Ans. d. All of the above
Answer
#Ans. a. True
Answer
#Ans. b. Slicing
Answer
#Ans. a. True
Answer
#Ans. b. 6
Answer
#Ans. a. empty string
Answer
#Ans. b. Complete String
Answer
#Ans. c. Step Size
Answer
#Ans. c. str1[n : m : k ]
Answer
#Ans. a. str1[ : : -1]
Answer
#Ans. c. both of the above
Answer
#Ans. c. title( )
Answer
#Ans. b. -1
Answer
#Ans. a. 2
Answer
#Ans. a. 2
Answer
#Ans. a. index( )
Answer
#Ans. c. Error
Answer
#Ans. a. True
Answer
#Ans. b. True
Answer
#Ans. a. False
Answer
#Ans. b. Hello World
Answer
#Ans. b. 2
Answer
#Ans. c. 3
Answer
#Ans. b. 2
Answer
#Ans. c. 5
Answer
#Ans. c. Both of the above
Answer
#Ans. c. -1
Answer
#Ans. d. Error
Answer
#Ans. a. True
Answer
#Ans. d. endswith( )
Answer
#Ans. a. True
Answer
#Ans. b. False
Answer
#Ans. a. TRUE
Answer
#Ans. b. True
Answer
#Ans. b. isupper( )
Answer
#Ans. b. ‘Hell* W*rld!’
Answer
#Ans. a. (‘India ‘, ‘is’, ‘ a Great is Country’)
Answer
#Ans. c. is
Answer
#Ans. c. Tuple
Answer
#Ans. c. 3
Answer
#Ans. a. List
Answer
#Ans. a. space
Answer
#Ans. d. ‘@’
Answer
#Ans. a. ‘p@y@t@h@o@n’
Answer
#Ans. b. 6
Answer
#Ans. b. lstrip( )
Answer
#Ans. d. All of the above
Answer
#Ans. d. find( )
Answer
#Ans. a. length( )
Answer
#Ans. b. lower( )