Unit-IV Strings MCQ
Unit-IV Strings MCQ
s = "python rocks"
print(s[1] * s.index("n"))
A. yyyyy
B. 55555
C. n
D. Error, you cannot combine all those things together.
Ans-D
Ans-A
Ans-A
s1 = "HEY"
s2 = s1.lower()
s3 = s2.capitalize()
A. Hey
B. hey
C. HEY
D. HEy
Ans- C
Mali = 5
print("Mali" + " is " + str(Mali))
A. Mali is Mali
B. Mali is 5
C. 5 is Mali
D. 5 is 5
Ans- B
s = "python rocks"
print(s[3])
A. t
B. h
C. c
D. Error, you cannot use the [ ] operator with a string.
Ans- B
s = "python rocks"
print(s[2] + s[-5])
A. tr
B. ps
C. nn
D. Error, you cannot use the [ ] operator with the + operator.
Ans-A
s = "python rocks"
print(len(s))
A. 11
B. 12
C. 10
D. 7
Ans-B
Q-9: What is printed by the following statements:
s = "Rose"
s[1] = "i"
print(s)
A. Rose
B. Rise
C. Error
D. Rsoe
Ans-C
s = "ball"
r = ""
for item in s:
r = item.upper() + r
Print(r)
A. Ball
B. BALL
C. LLAB
D. LAB
Ans-C
s = "python rocks"
print(s[7:11] * 3)
A. rockrockrock
B. rock rock rock
C. rocksrocksrocks
D. Error, you cannot use repetition with slicing.
Ans-A
A. True
B. False
Ans-B
Q-13: In Python, strings are…
A. str objects
B. Incorrect
C. changeable
D. Immutable
Ans-C
>>>"a"+"bc"
a) a
b) bc
c) bca
d) abc
Answer: d
Explanation: + operator is concatenation operator.
>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
Answer: c
Answer: b
Explanation: Execute in shell and check.
Q-16: What will be the output of the following Python code?
a) olleh
b) hello
c) h
d) o
Answer: d
Explanation: -1 corresponds to the last index.
Answer: c
Explanation: + is used to concatenate and * is used to multiply strings.
>>>print (r"\nhello")
Answer: b
Explanation: When prefixed with the letter ‘r’ or ‘R’ a string literal becomes a raw
string and the escape sequences such as \n are not converted.
>>>print('new' 'line')
a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line
Answer: c
Explanation: String literal separated by whitespace are allowed. They are
concatenated.
Q-20: What will be the output of the following Python statement?
>>> print('x\97\x98')
a) Error
b)
97
98
c) x\97
d) \x97\x98
Answer: c
Explanation: \x is an escape sequence that means the following 2 digits are a
hexadecimal number encoding a character.
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Answer: a
Explanation: Execute in shell to verify.
Answer: d
Explanation: 0xA and 0xB and 0xC are hexadecimal integer literals representing the
decimal values 10, 11 and 12 respectively. There sum is 33.
Q-23: What will be the output of above Python code?
str1="6/4"
print("str1")
A. 1
B. 6/4
C. 1.5
D. str1
Ans : D
Explanation: Since in print statement, str1 is written inside double quotes so it
will simply print str1 directly.
str1="python"
A. print(str1[2])
B. str1[1]="x"
C. print(str1[0:9])
D. Both (b) and (c)
Ans : B
Explanation: Strings are immutable. So,new values cannot be assigned at any
index position in a string
A. String is immutable.
B. capitalize() function in string is used to return a string by converting the whole
given string into uppercase.
C. lower() function in string is used to return a string by converting the whole given
string into lowercase.
D. None of these.
Ans : B
Explanation: capitalize() function in string gives the output by converting only the
first character of the string into uppercase and rest characters into
lowercase.However, upper() function is used to return the whole string into
uppercase.
Q-26: What will be the output of below Python code?
str1="Information"
print(str1[2:8])
A. format
B. formatio
C. orma
D. ormat
Ans : A
Explanation: Concept of slicing is used in this question. In string slicing,the
output is the substring starting from the first given index position i.e 2 to one less
than the second given index position i.e.(8-1=7) of the given string str1. Hence,
the output will be "format".
str1="Aplication"
str2=str1.replace('a','A')
print(str2)
A. application
B. Application
C. ApplicAtion
D. applicAtion
Ans : C
Explanation: replace() function in string is used here to replace all the existing "a"
by "A" in the given string.
Q-28: What will be the output of below Python code?
str1="poWer"
str1.upper()
print(str1)
A. POWER
B. Power
C. power
D. poWer
Ans : D
Explanation: str1.upper() returns the uppercase of whole string str1. However,it
doesnot change the string str1. So, output will be the original str1.
str1.find("save")
A. It returns the first index position of the first occurance of "save" in the given
string str1.
B. It returns the last index position of the last occurance of "save" in the given
string str1.
C. It returns the last index position of the first occurance of "save" in the given
string str1.
D. It returns the first index position of the first occurance of "save" in the given
string str1.
Ans : A
Explanation: It returns the first index position of the first occurance of "save" in
the given string str1.
Q-30:What will the below Python code will return?
list1=[0,2,5,1]
str1="7"
for i in list1:
str1=str1+i
print(str1)
A. 70251
B. 7
C. 15
D. Error
Ans : D
Explanation: list1 contains integers as its elements. Hence these cannot be
concatenated to string str1 by simple "+" operand. These should be converted to
string first by use of str() function,then only these will get concatenated.
If str1="John,Simon,Aryan"
A. print(str1[-7:-12])
B. print(str1[-11:-7])
C. print(str1[-11:-6])
D. print(str1[-7:-11])
Ans : C
Explanation: Slicing takes place at one index position less than the given second
index position of the string. So,second index position will be -7+1=-6.
Q-32: What will following Python code return?
str1="Stack of books"
print(len(str1))
A. 13
B. 14
C. 15
D. 16
Ans : B
Explanation: len() returns the length of the given string str1, including spaces and
considering " " as a single character.
print r"\nhello"
The output is
A. a new line and hello
B. \nhello
C. the letter r and then hello
D. Error
Ans: B
Answer: Option C
Explanation:
Cannot concantenate str and int objects.
Q-35:What is the output of the following?
print('ab'.isalpha())
A. True
B. False
C. None
D. Error
Answer: Option A
Explanation:
The string has only letters.
A - Hello World!
B-H
C - ello World!
Answer : B
Explanation
H is the correct answer.
Q-37: What is the output of print tinylist * 2 if tinylist = [123, 'john']?
B - [123, 'john'] * 2
C - Error
Answer : A
Explanation
It will print list two times. Output would be [123, 'john', 123, 'john'].
A - repr(x)
B - eval(str)
C - tuple(s)
D - list(s)
Answer : B
Explanation
eval(str) − Evaluates a string and returns an object.
Q-39:Which of the following function convert a single character to its integer
value in python?
A - unichr(x)
B - ord(x)
C - hex(x)
D - oct(x)
Answer : B
Explanation
ord(x) − Converts a single character to its integer value.
A - break
B - continue
C - pass
Answer : C
Explanation
pass statement − The pass statement in Python is used when a statement is
required syntactically but you do not want any command or code to execute.
Q-41 : Which of the following function checks in a string that all characters are in
uppercase?
A - isupper()
B - join(seq)
C - len(string)
D - ljust(width[, fillchar])
Answer : A
Explanation
isupper() − Returns true if string has at least 1 character and all characters are in
uppercase.
Q-42 : Which of the following function returns the min alphabetical character
from the string str?
A - lower()
B - lstrip()
C - max(str)
D - min(str)
Answer : D
Explanation
min(str) − Returns the min alphabetical character from the string str.
Q-43 : What is the output of ['Hi!'] * 4?
B - ['Hi!'] * 4
C - Error
Answer : A
Explanation
['Hi!', 'Hi!', 'Hi!', 'Hi!']
Q-44 : What is the following function removes last object from a list?
A - list.index(obj)
B - list.insert(index, obj)
C - list.pop(obj=list[-1])
D - list.remove(obj)
Answer : C
Explanation
list.pop(obj=list[-1]) − Removes and returns last object or obj from list.
Combined Extra Multiple Choice Questions
Answer : B
2. How to output the string “May the odds favor you” in Python?
Answer : A
Answer : B
Answer : D
Answer : A
Answer : D
Answer : B
Answer : C
Python:
1
2str = "My name is Kevin"
3
A. str[11:16]
B. str(11:16)
C. str[11][16]
D. str[11-16]
Answer : A
10. Which of the following is the correct way to access a specific character. Let’s
say we need to access the character “K” from the following string in Python?
1
2str = "My name is Kevin"
3
A. str(11)
B. str[11]
C. str:9
D. None of the above
Answer : B
11. Which Python module is used to parse dates in almost any string format?
A. datetime module
B. time module
C. calendar module
D. dateutil module
Answer : D
12. Which of the following is the correct way to indicate Hexadecimal Notation in
Python?
A. str = ‘\62’
B. str = ’62’
C. str = “62”
D. str = ‘\x62’
Answer : D
13. To begin slicing from the end of the string, which of the following is used in
Python?
A. Indexing
B. Negative Indexing
C. Begin with 0th index
D. Escape Characters
Answer : B
Answer : A
Answer : B
16. What is the correct way to get maximum value from Tuple in Python?
A. print (max(mytuple));
B. print (maximum(mytuple));
C. print (mytuple.max());
D. print (mytuple.maximum);
Answer : A
17. How to fetch and display only the keys of a Dictionary in Python?
A. print(mystock.keys())
B. print(mystock.key())
C. print(keys(mystock))
D. print(key(mystock))
Answer : A
19. How to access value for key “Price” in the following Python Dictionary:
1
2mystock = {
3"Product": "Earphone",
4"Price": 800,
5"Quantity": 50,
6"InStock" : "Yes"
7}
8
A. mystock[“Product”]
B. mystock(“Product”)
C. mystock[Product]
D. mystock(Product)
Answer : A
Answer : A
21. ___________ uses square brackets for comma-separated values in Python? Fill
Answer : A
Answer : A
23. How to find the index of the first occurrence of a specific value “i”, from string
“This is my website”?
A. str.find(“i”)
B. str.find(i)
C. str.index()
D. str.index(“i”)
Answer : D
Answer : B
25. Which of the following is the correcy way to access a specific element from a
Multi-Dimensional List?
A. list[row_size:column_size]
B. list[row_size][column_size]
C. list[(row_size)(column_size)]
D. None of the above
Answer : B
26. Which of the following Bitwise operators in Python shifts the left operand
value to the right, by the number of bits in the right operand. The rightmost bits
Answer : B
27. How to specify range of index in Python Tuples? Let’s say our tuple name is
“mytuple”
A. print(mytuple[1:4])
B. print(mytuple[1 to 4])
C. print(mytuple[1-4])
D. print(mytuple[].slice[1:4])
Answer : A
Answer : B
Answer : A
Answer : C
31. How to delete an element in a Dictionary with a specific key. Let’s say we need
Answer : B
Answer : A
34. Which of the following is used to empty the Dictionary in Python? Let’s say
Answer : D
Answer : A
36. What is used in Python functions, if you have no idea about the number of
arguments to be passed?
A. Keyword Arguments
B. Default Arguments
C. Required Arguments
D. Arbitrary Arguments
Answer : D
37. What is the correct way to create a Dictionary in Python?
A. mystock = {“Product”: “Earphone”, “Price”: 800, “Quantity”: 50}
B. mystock = {“Product”- “Earphone”, “Price”-800, “Quantity”-50}
C. mystock = {Product[Earphone], Price[800], Quantity[50]}
D. None of the above
Answer : A
38. In Python Functions, what is to be used to skip arguments or even pass them in
Answer : A
Answer : C
Answer : C
Answer : B
42. How to convert the lowercase letters in the string to uppercase in Python?
A. upper()
B. uppercase()
C. capitalize()
D. toUpper()
Answer : A
43. What is the correct way to get minimum value from a List in Python?
A. print (minimum(mylist));
B. print (min(mylist));
C. print (mylist.min());
D. print (mylist.minimum());
Answer : B
Answer : A
Answer : A
Answer : C
47. How to compare two operands in Python and check for equality? Which
operator is to be used?
A. =
B. in operator
C. is operator
D. == (Answer)
Answer : D
48. What is the correct way to get minimum value from Tuple?
A. print (min(mytuple));
B. print (minimum(mytuple));
C. print (mytuple.min());
D. print (mytuple.minimum);
Answer : A
Answer : D
50. How to compare two objects and check whether they have same memory
locations?
A. is operator
B. in operator
C. **
D. Bitwise operators
Answer : A
51. How to fetch and display only the values of a Dictionary in Python?
A. print(mystock.value())
B. print(mystock.values())
C. print(values(mystock))
D. print(value(mystock))
Answer : A
Answer : B
Answer : A
Answer : C
55. Which of the following Bitwise operators sets each bit to 1, if only one of them
Answer : A
56. What is the correct way to get maximum value from a List in Python?
A. print (maximum(mylist));
B. print (mylist.max());
C. print (max(mylist));
D. print (mylist.maximum());
Answer : C
57. An example to correctly begin searching from the end range of index in
Answer : D
58. Which of the following Bitwise operators in Python shifts the left operand
value to the left, by the number of bits in the right operand. The leftmost bits
Answer : C
59. Can we update Tuples or any of its elements in Python after assignment?
A. Yes
B. No
Answer : A
60. How to display only the current month’s name in Python?
A. date.strftime(“%H”)
B. date.strftime(“%B”)
C. date.strftime(“%m”)
D. date.strftime(“%d”)
Answer : B
Answer : B
Answer : A
Answer : B
Answer : B
65. What is the correct way to create a list with type float?
A. mylist = [5.7, 8.2, 9. 3.8, 2.9]
B. mylist = (5.7, 8.2, 9. 3.8, 2.9)
C. mylist = {5.7, 8.2, 9. 3.8, 2.9}
D. None of the above
Answer : A
Answer : A
Answer : A
68. How to fetch last element from a Python List with negative indexing?
A. print(“Last element = “,mylist[0])
B. print(“Last element = “,mylist[])
C. print(“Last element = “,mylist[-1])
D. None of the above
Answer : C
69. What is the correct way to get the length of a List in Python?
A. mylist.count()
B. count(mylist)
C. len(mylist)
D. length(mylist)
Answer : C
70. To get today’s date, which Python module is to be imported?
A. calendar module
B. datetime module
C. dateutil module
D. None of the above
Answer : B