Python Multiple Choice Questions on “Strings – 1”.
1. What will be the output of the following Python statement?
a) a
b) bc
c) bca
d) abc
Answer: d
Clarification: + operator is concatenation operator.
2. What will be the output of the following Python statement?
a) a
b) ab
c) cd
d) dc
Answer: c
Clarification: 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_uppercase
c) string.letters
d) string.lowercase_string.uppercase
Answer: b
Clarification: 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
Answer: d
Clarification: -1 corresponds to the last index.
5. What arithmetic operators cannot be used with strings?
a) +
b) *
c) –
d) All of the mentioned
Answer: c
Clarification: + is used to concatenate and * is used to multiply strings.
6. What will be the output of the following Python code?
a) a new line and hello
b) nhello
c) the letter r and then hello
d) error
Answer: b
Clarification: 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?
a) Error
b) Output equivalent to print ‘newnline’
c) newline
d) new line
Answer: c
Clarification: String literal separated by whitespace are allowed. They are concatenated.
8. What will be the output of the following Python statement?
a) Error
b)
97 98
c) x97
d) x97x98
Answer: c
Clarification: 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
Answer: a
Clarification: Execute in shell to verify.
10. What will be the output of the following Python code?
a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
Answer: d
Clarification: 0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. There sum is 33.