Python MCQs on String: SET 2

11. What will be the output of the following Python code?

class father:

def __init__(self, param):

self.o1 = param

class child(father):

def __init__(self, param):

self.o2 = param

>>>obj = child(22)

>>>print “%d %d” % (obj.o1, obj.o2)

a) None None

b) None 22

c) 22 None

d) Error is generated

Check Answer

Answer: d
self.o1 was never created.

 

 

12. What will be the output of the following Python code?

class tester:

def __init__(self, id):

self.id = str(id)

id=”224″

>>>temp = tester(12)

>>>print(temp.id)

a) 224

b) Error

c) 12

d) None

Check Answer
Answer: c
Id in this case will be the attribute of the class.

 

 

13. What will be the output of the following Python code?

>>>example = “snow world”

>>>print(“%s” % example[4:7])

a) wo

b) world

c) sn

d) rl

Check Answer

Answer: a
Execute in the shell and verify.

 

 

14. What will be the output of the following Python code?

>>>example = “snow world”

>>>example[3] = ‘s’

>>>print example

a) snow

b) snow world

c) Error

d) snos world

Check Answer

Answer: c
Strings cannot be modified.

 

 

15. What will be the output of the following Python code?

>>>max(“what are you”)

a) error

b) u

c) t

d) y

Check Answer

Answer: d
Max returns the character with the highest ascii value.

 

 

16. Given a string example=”hello” what is the output of example.count(‘l’)?

a) 2

b) 1

c) None

d) 0

Check Answer
Answer: a
l occurs twice in hello.

 

 

17. What will be the output of the following Python code?

>>>example = “helle”

>>>example.find(“e”)

a) Error

b) -1

c) 1

d) 0

Check Answer

Answer: c
Returns lowest index.

 

 

18. What will be the output of the following Python code?

>>>example = “helle”

>>>example.rfind(“e”)

a) -1

b) 4

c) 3

d) 1

Check Answer

Answer: b
Returns highest index.

 

 

19. What will be the output of the following Python code?

>>>example=”helloworld”

>>>example[::-1].startswith(“d”)

a) dlrowolleh

b) True

c) -1

d) None

Check Answer
Answer: b
Starts with checks if the given string starts with the parameter that is passed.

 

 

20. To concatenate two strings to a third what statements are applicable?

a) s3 = s1 . s2

b) s3 = s1.add(s2)

c) s3 = s1.__add__(s2)

d) s3 = s1 * s2

Check Answer

Answer: c
__add__ is another method that can be used for concatenation.

 

Pages  1  2  3  4  5  6  7  8  9  10  11  12  13

 

 

 

 

 

Leave a Comment