0% found this document useful (0 votes)
364 views32 pages

Unit-IV Strings MCQ

This document contains 40 multiple choice questions about strings in Python. The questions cover various string operations and methods like slicing, indexing, concatenation, formatting, finding, replacing, converting case etc. The answers to each question are also provided to check understanding of string concepts in Python.

Uploaded by

Durgesh Dhore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
364 views32 pages

Unit-IV Strings MCQ

This document contains 40 multiple choice questions about strings in Python. The questions cover various string operations and methods like slicing, indexing, concatenation, formatting, finding, replacing, converting case etc. The answers to each question are also provided to check understanding of string concepts in Python.

Uploaded by

Durgesh Dhore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

UNIT-IV STRINGS

Multiple Choice Questions

Q-1: What is printed by the following statements?

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

Q-2: What will be printed when the following executes?

str = "His shirt is red"


pos = str.find("is")
print(pos)
A. 1
B. 9
C. 10
D. 6

Ans-A

Q-3: What will be printed when the following executes?

str = "This is the end"


str = str[5]
print(str)
A. i
B. s
C. is the end
D. t

Ans-A

Q-4: What is the value of s1 after the following code executes?

s1 = "HEY"
s2 = s1.lower()
s3 = s2.capitalize()
A. Hey
B. hey
C. HEY
D. HEy
Ans- C

Q-5: What would the following code print?

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

Q-6: What is printed by the following statements?

s = "python rocks"
print(s[3])
A. t
B. h
C. c
D. Error, you cannot use the [ ] operator with a string.

Ans- B

Q-7: What is printed by the following statements?

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

Q-8: What is printed by the following statements?

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

Q-10: What is printed by the following statements:

s = "ball"
r = ""
for item in s:
r = item.upper() + r
Print(r)

A. Ball
B. BALL
C. LLAB
D. LAB

Ans-C

Q-11: What is printed by the following statements?

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

Q-12: What is the output of the following string operations

str = "My salary is 7000";


print(str.isalnum())

A. True
B. False

Ans-B
Q-13: In Python, strings are…

A. str objects

B. Incorrect

C. changeable

D. Immutable

Ans-C

Q-14: What will be the output of the following Python statement?

>>>"a"+"bc"
a) a
b) bc
c) bca
d) abc

Answer: d
Explanation: + operator is concatenation operator.

Q-14: What will be the output of the following Python statement?

>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc

Answer: c

Explanation: Slice operation is performed on string.

Q-15: The output of executing string.ascii_letters can also be achieved by:


a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_upercase
c) string.letters
d) string.lowercase_string.upercase

Answer: b
Explanation: Execute in shell and check.
Q-16: What will be the output of the following Python code?

>>> str1 = 'hello'


>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]

a) olleh
b) hello
c) h
d) o

Answer: d
Explanation: -1 corresponds to the last index.

Q-17: What arithmetic operators cannot be used with strings?


a) +
b) *
c) –
d) All of the mentioned

Answer: c
Explanation: + is used to concatenate and * is used to multiply strings.

Q-18: What will be the output of the following Python code?

>>>print (r"\nhello")

a) a new line and hello


b) \nhello
c) the letter r and then hello
d) error

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.

Q-19: What will be the output of the following Python statement?

>>>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.

Q-21: What will be the output of the following Python code?

>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld

Answer: a
Explanation: Execute in shell to verify.

Q-22: What will be the output of the following Python code?

print(0xA + 0xB + 0xC)


a) 0xA0xB0xC
b) Error
c) 0x22
d) 33

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.

Q-24: Which of the following will result in an error?

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

Q-25: Which of the following is False?

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".

Q-27: What will be the output of below Python code?

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.

Q-29:What will the below Python code will return?

If str1="save paper,save plants"

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.

Q-31: Which of the following will give "Simon" as output?

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.

Q-33: What is the output when following code is executed ?

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

Q-34: What is the output of “hello”+1+2+3 ?


A. hello123
B. hello
C. Error
D. hello6

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.

Q-36: What is the output of print str[0] if str = 'Hello World!'?

A - Hello World!

B-H

C - ello World!

D - None of the above.

Answer : B
Explanation
H is the correct answer.
Q-37: What is the output of print tinylist * 2 if tinylist = [123, 'john']?

A - [123, 'john', 123, 'john']

B - [123, 'john'] * 2

C - Error

D - None of the above.

Answer : A
Explanation
It will print list two times. Output would be [123, 'john', 123, 'john'].

Q-38:Which of the following function convert a String to an object in python?

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.

Q-40 : Which of the following statement is used when a statement is required


syntactically but you do not want any command or code to execute?

A - break

B - continue

C - pass

D - None of the above.

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?

A - ['Hi!', 'Hi!', 'Hi!', 'Hi!']

B - ['Hi!'] * 4

C - Error

D - None of the above.

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

1. Do we need to compiler a program before execution in Python?


A. Yes
B. No

Answer : B

2. How to output the string “May the odds favor you” in Python?

A. print(“May the odds favor you”)


B.echo(“May the odds favor you”)
C.System.out(“May the odds favor you”)
D.printf(“May the odds favor you”)

Answer : A

3. How to create a variable in Python with value 22.6?


A. int a = 22.6
B. a = 22.6
C. Integer a = 22.6
D. None of the above

Answer : B

4. How to add single-line comment in Python?


A. /* This is comment */
B. !! This is comment
C. // This is comment
D. # This is comment

Answer : D

5. How to represent 261500000 as a floating number in Python?


A. 2.615E8
B. 261500000
C. 261.5E8
D. 2.6

Answer : A

6. Select the correct example of complex datatype in Python


A. 3 + 2j
B. -100j
C. 5j
D. All of the above are correct

Answer : D

7. What is the correct way of creating a multi-line string in Python?


A. str = “”My name is Kevin and I
live in New York””
B. str = “””My name is Kevin and I
live in New York”””
C. str = “My name is Kevin and I
live in New York”
D. All of the above

Answer : B

8. How to convert the uppercase letters in the string to lowercase in Python?


A. lowercase()
B. capilaize()
C. lower()
D. toLower()

Answer : C

9. How to access substring “Kevin” from the following string declaration in

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

14. How to fetch characters from a given range in Python?


A. [:]
B. in operator
C. []
D. None of the above

Answer : A

15. How to capitalize only the first letter of a sentence in Python?


A. uppercase() method
B. capitalize() method
C. upper() method
D. None of the above

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

18. How to align a string centrally in Python?


A. align() method
B. center() method
C. fill() method
D. None of the above
Answer : B

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

20. How to set the tab size to 6 in Python Strings?


A. expandtabs(6)
B. tabs(6)
C. expand(6)
D. None of the above

Answer : A

21. ___________ uses square brackets for comma-separated values in Python? Fill

in the blanks with a Python collection?


A. Lists
B. Dictionary
C. Tuples
D. None of the above

Answer : A

22. Are Python Tuples faster than Lists?


A. TRUE
B. FALSE

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

24. How to display whether the date is AM/PM in Python?


A. Use the %H format code
B. Use the %p format code
C. Use the %y format code
D. Use the %I format code

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

while shifting fall off?


A. Bitwise XOR Operator
B. Bitwise Right Shift Operator
C. Bitwise Left Shift Operator
D. None of the Above

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

28. How to correctly create a function in Python?


A. demoFunction()
B. def demoFunction()
C. function demoFunction()
D. void demoFunction()

Answer : B

29. Which one of the following is a valid identifier declaration in Python?


A. str = “demo666”
B. str = “666”
C. str = “demo demo2”
D. str = “$$$666”

Answer : A

30. How to check whether all the characters in a string is printable?


A. print() method
B. printable() method
C. isprintable() method
D. echo() method

Answer : C

31. How to delete an element in a Dictionary with a specific key. Let’s say we need

to delete the key “Price”


A. del dict[‘Price’]
B. del.dict[‘Price’]
C. del dict(‘Price’)
D. del.dict[‘Price’]
Answer : A
32. How to swap case in Python i.e. lowercase to uppercase and vice versa?
A. casefold() method
B. swapcase() method
C. case() method
D. title() method

Answer : B

33. The def keyword is used in Python to?


A. Define a function name
B. Define a list
C. Define an array
D. Define a Dictionary

Answer : A

34. Which of the following is used to empty the Dictionary in Python? Let’s say

our dictionary name is “mystock”.


A. mystock.del()
B. clear mystock
C. del mystock
D. mystock.clear()

Answer : D

35. How to display only the day number of year in Python?


A. date.strftime(“%j”)
B. date.strftime(“%H”)
C. date.strftime(“%I”)
D. date.strftime(“%p”)

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

any order while calling a function?


A. Keyword arguments
B. Default Arguments
C. Required Arguments
D. Arbitrary Arguments

Answer : A

39. How to get the total length of Tuple in Python?


A. count() method
B. length() method
C. len() method
D. None of the above

Answer : C

40. How to access a value in Tuple?


A. mytuple(1)
B. mytuple{1}
C. mytuple[1]
D. None of the above

Answer : C

41. What is to be used to create a Dictionary of arguments in a Python function?


A. Arbitrary arguments in Python (*args)
B. Arbitrary keyword arguments in Python (**args)
C. Keyword Arguments
D. Default Arguments

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

44. Is using return statement optional in a Python Function?


A. Yes
B. No

Answer : A

45. Which of the following correctly converts int to float in Python?


A. res = float(10)
B. res = convertToFloat(10)
C. None of the above

Answer : A

46. How to get the type of a variable in Python?


A. print(typeOf(a))
B. print(typeof(a))
C. print(type(a))
D. None of the above

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

49. How to display only the current month’s number in Python?


A. date.strftime(“%H”)
B. date.strftime(“%I”)
C. date.strftime(“%p”)
D. date.strftime(“%m”)

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

52. Which operator is used in Python to raise numbers to the power?


A. Bitwise Operators
B. Exponentiation Operator (**)
C. Identity Operator (is)
D. Membership Operators (in)

Answer : B

53. Can we create a Tuple in Python without parenthesis?


A. TRUE
B. FALSE

Answer : A

54. ____________ represents key-value pair in Python?


A. Tuples
B. Lists
C. Dictionary
D. All the Above

Answer : C

55. Which of the following Bitwise operators sets each bit to 1, if only one of them

is 1, i.e. if only one of the two operands are 1.


A. Bitwise XOR
B. Bitwise OR
C. Bitwise AND
D. Bitwise NOT

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

Python Tuples. Let’s say our Python Tuple has 4 elements


A. print(mytuple[-3 to -1])
B. print(mytuple[3-1])
C. print(mytuple[].slice[-3:-1])
D. print(mytuple[-3:-1])

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

while shifting fall off.


A. Bitwise XOR Operator
B. Bitwise Right Shift Operator
C. Bitwise Left Shift Operator
D. None of the Above

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

61. ___________ uses Parenthesis for comma-separated values in Python? Fill in

the blanks with a Python collection?


A. List
B. Tuples
C. None of the above

Answer : B

62. How to create an empty Tuple in Python?


A. mytuple = ()
B. mytuple = (0)
C. mytuple = 0
D. mytuple =

Answer : A

63. Can we have duplicate keys in Python Dictionary?


A. TRUE
B. FALSE

Answer : B

64. Lists in Python are Immutable?


A. TRUE
B. FALSE

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

66. How to display only the day number of year in Python?


A. date.strftime(“%j”)
B. date.strftime(“%H”)
C. date.strftime(“%I”)
D. date.strftime(“%p”)

Answer : A

67. Delete multiple elements in a range from a Python List


A. del mylist[2:4]
B. del mylist(2:4)
C. del mylist[2 to 4]
D. del mylist(2 to 4)

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

You might also like