Python MCQs on String: SET 3

21. What will be the output of the following Python statement?

>>>chr(ord(‘A’))

a) A

b) B

c) a

d) Error

Check Answer

Answer: a
Execute in shell to verify.

 

 

22. What will be the output of the following Python statement?

>>>print(chr(ord(‘b’)+1))

a) a

b) b

c) c

d) A

Check Answer

Answer: c
Execute in the shell to verify.

 

 

23. Which of the following statement prints hello\example\test.txt?

a) print(“hello\example\test.txt”)

b) print(“hello\\example\\test.txt”)

c) print(“hello\”example\”test.txt”)

d) print(“hello”\example”\test.txt”)

Check Answer

 

Answer: b
\is used to indicate that the next \ is not an escape sequence.

 

24. Suppose s is “\t\tWorld\n”, what is s.strip()?

a) \t\tWorld\n

b) \t\tWorld\n

c) \t\tWORLD\n

d) World

Check Answer
Answer: d
Execute help(string.strip) to find details.

 

 

25. The format function, when applied on a string returns ___________

a) Error

b) int

c) bool

d) str

Check Answer

Answer: d
Format function returns a string.

 

 

26. What will be the output of the “hello” +1+2+3?

a) hello123

b) hello

c) Error

d) hello6

Check Answer

 

Answer: c
Cannot concatenate str and int objects.

 

 

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

>>>print(“D”, end = ‘ ‘)

>>>print(“C”, end = ‘ ‘)

>>>print(“B”, end = ‘ ‘)

>>>print(“A”, end = ‘ ‘)

a) DCBA

b) A, B, C, D

c) D C B A

d) D, C, B, A will be displayed on four lines

Check Answer

Answer: c
Execute in the shell.

 

 

28. What will be the output of the following Python statement?(python 3.xx)

>>>print(format(“Welcome”, “10s”), end = ‘#’)

>>>print(format(111, “4d”), end = ‘#’)

>>>print(format(924.656, “3.2f”))

a) Welcome# 111#924.66

b) Welcome#111#924.66

c) Welcome#111#.66

d) Welcome # 111#924.66

Check Answer

 

Answer: d
Execute in the shell to verify.

 

29. What will be displayed by print(ord(‘b’) – ord(‘a’))?

a) 0

b) 1

c) -1

d) 2

Check Answer

Answer: b
ASCII value of b is one more than a. Hence the output of this code is 98-97, which is equal to 1.

 

 

30. Say s=”hello” what will be the return value of type(s)?

a) int

b) bool

c) str

d) String

Check Answer

Answer: c
str is used to represent strings in python.

 

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

 

 

 

 

Leave a Comment