
Q 1. The output of executing string.ascii_letters can also be achieved by:
A. string.ascii_lowercase_string.digits
B. string.lowercase_string.uppercase
C. string.letters
D. string.ascii_lowercase+string.ascii_uppercase
Show Answer
Answer:-D. string.ascii_lowercase+string.ascii_uppercaseExplanation
Execute in shell and check.Q 2 . 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
Show Answer
Answer:-D. oExplanation
1 corresponds to the last index.Q 3. What arithmetic operators cannot be used with strings?
A. +
B. *
C. –
D. All of the mentioned
Show Answer
Answer:-C. –Explanation
+ is used to concatenate and * is used to multiply strings.Q 4. 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
Show Answer
Answer:-B. \nhelloExplanation
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.Q 5. What will be the output of the following Python statement?
- >>>print(‘new’ ‘line’)
A. Error
B. Output equivalent to print ‘new\nline’
C. new line
D. newline
Show Answer
Answer:-D. newlineExplanation
String literal separated by whitespace are allowed. They are concatenated.Q 6. What will be the output of the following Python statement?
- >>> print(‘x\97\x98‘)
A. Error
B. 97 98
C. \x97\x98
D. x\97
Show Answer
Answer:-D. x\97Explanation
\x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character.Q 7. What will be the output of the following Python code?
- >>>str1=”helloworld”
- >>>str1[::-1]
A. world
B. hello
C. dlrowolleh
D. helloworld
Show Answer
Answer:-C. dlrowollehExplanation
Execute in shell to verify.Q 8. What will be the output of the following Python code?
print(0xA + 0xB + 0xC)
A. 0xA0xB0xC
B. Error
C. 0x22
D. 33
Show Answer
Answer:-D. 33Explanation
0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. There sum is 33.Q 9. What will be the output of the following Python statement?
- >>>”abcd”[2:]
A. a
B. ab
C. cd
D. dc
Show Answer
Answer:-C. cdExplanation
Slice operation is performed on string.Q 10. What will be the output of the following Python statement?
- >>>”a”+”bc”
A. a
B. bc
C. bca
D. abc
Leave a Reply