Python MCQs on String: SET 1

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

>>”a”+”bc”

a) a

b) bc

c) bca

d) abc

Check Answer

Answer: d
+ operator is concatenation operator.

 

 

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

>>”abcd”[2:]

a) a

b) ab

c) cd

d) dc

Check Answer

Answer: c
Slice operation is performed on string.

 

 

 

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

Check Answer

 

Answer: b
Execute in shell and check.

 

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

Check Answer

Answer: d
-1 corresponds to the last index.

 

 

5. What arithmetic operators cannot be used with strings?

a) +

b) *

c) –

d) All of the mentioned

Check Answer

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

 

 

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

Check Answer

Answer: b
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.

 

 

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

Check Answer

Answer: c
String literal separated by whitespace are allowed. They are concatenated.

 

 

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

Check Answer

Answer: c
\x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character.

 

 

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

>>>str1=”helloworld”

>>>str1[::-1]

a) dlrowolleh

b) hello

c) world

d) helloworld

Check Answer

Answer: a
Execute in shell to verify.

 

 

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

print(0xA + 0xB + 0xC)

a) 0xA0xB0xC

b) Error

c) 0x22

d) 33

Check Answer
Answer: d
0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. There sum is 33.

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

 

 

 

Leave a Comment