121). 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’
Show Answer
Correct Answer: ‘first=H, third=L’
Notes:
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’].
122). 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
Show Answer
Correct Answer: “a=H, b=O, c=[‘E’, ‘L’]”
Notes:
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.
123). The formatting method {1:<10} represents the ___________ positional argument, _________ justified in a 10 character wide field.
l=list('HELLO')
p=l[0], l[-1], l[1:3]
'a={0}, b={1}, c={2}'.format(*p)
[A]first, right
[B]second, left
[C]first, left
[D]second, right
Show Answer
Correct Answer: second, left
Notes:
Answer: b
Explanation: The formatting method {1:<10} represents the second positional argument, left justified in a 10 character wide field.
124). 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)
Show Answer
Correct Answer: (‘0xff’, 255, 255)
Notes:
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).
125). The output of the two codes shown below is the same.
bin((2**16)-1)
'{}'.format(bin((2**16)-1))
[A]True
[B]False
[C]1
[D]0
Show Answer
Correct Answer: True
Notes:
Answer: a
Explanation: The output of both of the codes shown above is ‘0b1111111111111111’. Hence the statement is true.
126). What will be the output of the following Python code?
'{a}{b}{a}'.format(a='hello', b='world')
[A]‘hello world’
[B]‘hello’ ‘world’ ‘hello’
[C]‘helloworldhello’
[D]‘hello’ ‘hello’ ‘world’
Show Answer
Correct Answer: ‘helloworldhello’
Notes:
Answer: c
Explanation: The code shown above prints the values substituted for a, b, a, in the same order. This operation is performed using the format function. Hence the output of the code is: ‘helloworldhello’.
127). What will be the output of the following Python code?
D=dict(p='san', q='jay')
'{p}{q}'.format(**D)
[A]Error
[B]sanjay
[C]san jay
[D]{‘san’, ‘jay’}
Show Answer
Correct Answer: sanjay
Notes:
Answer: b
Explanation: The code shown above prints the values substituted for p and q in the same order. Note that there is no blank space between p and q. Hence the output is: sanjay.
128). What will be the output of the following Python code?
'The {} side {1} {2}'.format('bright', 'of', 'life')
[A]Error
[B]‘The bright side of life’
[C]‘The {bright} side {of} {life}’
[D]No output
Show Answer
Correct Answer: Error
Notes:
Answer: a
Explanation: The code shown above results in an error. This is because we have switched from automatic field numbering to manual field numbering, that is, from {} to {1}. Hence this code results in an error.
129). What will be the output of the following Python code?
'{0:f}, {1:2f}, {2:05.2f}'.format(1.23456, 1.23456, 1.23456)
[A]Error
[B]‘1.234560, 1.22345, 1.23’
[C]No output
[D]‘1.234560, 1.234560, 01.23’
Show Answer
Correct Answer: ‘1.234560, 1.234560, 01.23’
Notes:
Answer: d
Explanation: In the code shown above, various formatting options are displayed using the format option. Hence the output of this code is: ‘1.234560, 1.234560, 01.23’
130). What will be the output of the following Python code?
'%.2f%s' % (1.2345, 99)
[A]‘1.2345’, ‘99’
[B]‘1.2399’
[C]‘1.234599’
[D]1.23, 99
Show Answer
Correct Answer: ‘1.2399’
Notes:
Answer: b
Explanation: In this code, we must notice that since multiple values haven been given, they should be enclosed in a tuple. Since the formatting format is %.2f, the value 1.2345 is reduced to two decimal places. Hence the output of the code shown above: ‘1.2399’.
131). What will be the output of the following Python code?
'%s' %((1.23,),)
[A]‘(1.23,)’
[B]1.23,
[C](,1.23)
[D]‘1.23’
Show Answer
Correct Answer: ‘(1.23,)’
Notes:
Answer: a
Explanation: The formatting expression accepts either a single substitution value, or a tuple of one or more items. Since single item can be given either by itself or within the tuple, a tuple to be formatted must be provided as a tested tuple. Hence the output of the code is: >>> ‘%s’ %((1.23,),).
132). What will be the output of the following two codes?
i. '{0}'.format(4.56)
ii. '{0}'.format([4.56,])
[A] ‘4.56’, ‘4.56,’
[B]‘4.56’, ‘[4.56]’
[C]4.56, [4.56,]
[D]4.56, [4.56,]
Show Answer
Correct Answer: ‘4.56’, ‘[4.56]’
Notes:
Answer: b
Explanation: The code shown above shows the formatting option on the same value, that is 4.56, where in the second case, the value is enclosed in a list. Hence the output of the code shown above is:
‘4.56’, ‘[4.56]’