Python Questions and Answers Part-8

1. What will be the output of the following Python code?
t = '%(a)s, %(b)s, %(c)s'
t % dict(a='hello', b='world', c='universe')
a) ‘hello, world, universe’
b) ‘hellos, worlds, universes’
c) Error
d) hellos, world, universe

Answer: a
Explanation: Within the subject string, curly braces represent substitution targets and arguments to be inserted. Hence the output of the code shown above: ‘hello, world, universe’.

2. What will be the output of the following Python code?
'{a}, {0}, {abc}'.format(10, a=2.5, abc=[1, 2])
a) Error
b) ‘2.5, 10, [1, 2]’
c) 2.5, 10, 1, 2
d) ’10, 2.5, [1, 2]’

Answer: b
Explanation: Since we have specified that the order of the output be: {a}, {0}, {abc}, hence the value of associated with {a} is printed first followed by that of {0} and {abc}. Hence the output of the code shown above is: ‘2.5, 10, [1, 2]’.

3. What will be the output of the following Python code?
'{0:.2f}'.format(1.234)
a) ‘1’
b) ‘1.234’
c) ‘1.23’
d) ‘1.2’

Answer: c
Explanation: The code shown above displays the string method to round off a given decimal number to two decimal places. Hence the output of the code is: ‘1.23’.

4. What will be the output of the following Python code?
'%x %d' %(255, 255)
a) ‘ff, 255’
b) ‘255, 255’
c) ‘15f, 15f’
d) Error

Answer: a
Explanation: The code shown above converts the given arguments to hexadecimal and decimal values and prints the result. This is done using the format specifiers %x and %d respectively. Hence the output of the code shown above is: ‘ff, 255’.

5. The output of the two codes shown below is the same.
i. '{0:.2f}'.format(1/3.0)
ii. '%.2f'%(1/3.0)
a) True
b) False

Answer: a
Explanation: The two codes shown above represent the same operation but in different formats. The output of both of these functions is: ‘0.33’. Hence the statement is true.

6. What will be the output of the following Python code?
l=list('HELLO')
'first={0[0]}, third={0[2]}'.format(l)
a) ‘first=H, third=L’
b) ‘first=0, third=2’
c) Error
d) ‘first=0, third=L’

Answer: a
Explanation: In the code shown above, the value for first is substituted by l[0], that is H and the value for third is substituted by l[2], that is L. Hence the output of the code shown above is: ‘first=H, third=L’. The list l= [‘H’, ‘E’, ‘L’, ‘L’, ‘O’].

7. What will be the output of the following Python code?
l=list('HELLO')
p=l[0], l[-1], l[1:3]
'a={0}, b={1}, c={2}'.format(*p)
a) Error
b) “a=’H’, b=’O’, c=(E, L)”
c) “a=H, b=O, c=[‘E’, ‘L’]”
d) Junk value

Answer: c
Explanation: In the code shown above, the value for a is substituted by l[0], that is ‘H’, the value of b is substituted by l[-1], that is ‘O’ and the value for c is substituted by l[1:3]. Here the use of *p is to unpack a tuple items into individual function arguments.

8. The formatting method {1:<10} represents the ___________ positional argument, _________ justified in a 10 character wide field.
a) first, right
b) second, left
c) first, left
d) second, right

Answer: b
Explanation: The formatting method {1:<10} represents the second positional argument, left justified in a 10 character wide field.

9. What will be the output of the following Python code?
hex(255), int('FF', 16), 0xFF
a) [0xFF, 255, 16, 255]
b) (‘0xff’, 155, 16, 255)
c) Error
d) (‘0xff’, 255, 255)

Answer: d
Explanation: The code shown above converts the value 255 into hexadecimal, that is, 0xff. The value ‘FF’ into integer. Hence the output of the code shown is: (‘0xff’, 255, 255).

10. The output of the two codes shown below is the same.
i. bin((2**16)-1)
ii. '{}'.format(bin((2**16)-1))
a) True
b) False

Answer: a
Explanation: The output of both of the codes shown above is ‘0b1111111111111111’. Hence the statement is true.