1. 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
Discussion
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.
2. 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
Discussion
Explanation: String literal separated by whitespace are allowed. They are concatenated.
3. 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
Discussion
Explanation: \x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character.
4. What will be the output of the following Python code?
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Discussion
5. What will be the output of the following Python code?
print(0xA + 0xB + 0xC)
a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
Discussion
Explanation: 0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. There sum is 33.
6. What is “Hello”.replace(“l”, “e”)?
a) Heeeo
b) Heelo
c) Heleo
d) None
Discussion
Explanation: Execute in shell to verify.
7. To retrieve the character at index 3 from string s=”Hello” what command do we execute ?
a) s[]
b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
Discussion
Explanation: __getitem(..) can be used to get character at index specified as parameter.
8. To return the length of string s what command do we execute?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
Discussion
9. To check whether string s1 contains another string s2, use ________
a) s1.__contains__(s2)
b) s2 in s1
c) s1.contains(s2)
d) si.in(s2)
Discussion
Explanation: s2 in s1 works in the same way as calling the special function __contains__ .
10. What function do you use to read a string?
a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))
Discussion