This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Strings – 1”.
1. What is the output when following statement is executed ?
Explanation: + operator is concatenation operator.
2. What is the output when following statement is executed ?
Explanation: Slice operation is performed on string.
3. The output of executing string.ascii_letters can also be achieved by:
Explanation: Execute in shell and check.
4. What is the output when following code is executed ?
>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
Explanation: -1 corresponds to the last index.
5. What arithmetic operators cannot be used with strings ?
d) All of the mentioned
Explanation: + is used to concatenate and * is used to multiply strings.
6. What is the output when following code is executed ?
The output is
a) a new line and hello
c) the letter r and then hello
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.
7. What is the output when following statement is executed ?
b) Output equivalent to print ‘new\nline’
d) new line
Explanation: String literals seperated by white space are allowed. They are concatenated.
8. What is the output when following statement is executed ?
Explanation: \x is an escape sequence that means the following 2 digits are a hexadicmal number encoding a character.
9. What is the output when following code is executed ?
Explanation: Execute in shell to verify.
10. print(0xA + 0xB + 0xC) :
Explanation: 0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10,11 and 12 respectively. There sum is 33.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.