280). What will be the output of the following Python code?
print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))
[A]Hello foo and bin
[B]Hello {name1} and {name2}
[C]Error
[D]Hello and
Show Answer
Correct Answer: Hello foo and bin
Notes:
Answer: a
Explanation: The arguments are accessed by their names.
281). What will be the output of the following Python code?
print("Hello {0!r} and {0!s}".format('foo', 'bin'))
[A]Hello foo and foo
[B]Hello ‘foo’ and foo
[C]Hello foo and ‘bin’
[D]Error
Show Answer
Correct Answer: Hello ‘foo’ and foo
Notes:
Answer: b
Explanation: !r causes the characters ‘ or ” to be printed as well.
282). What will be the output of the following Python code?
print("Hello {0} and {1}".format(('foo', 'bin')))
[A]Hello foo and bin
[B]Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
[C]Error
[D]None of the mentioned
Show Answer
Correct Answer: Error
Notes:
Answer: c
Explanation: IndexError, the tuple index is out of range.
283). What will be the output of the following Python code?
print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))
[A]Hello foo and bin
[B]Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
[C]Error
[D]None of the mentioned
Show Answer
Correct Answer: Hello foo and bin
Notes:
Answer: a
Explanation: The elements of the tuple are accessed by their indices.
284). What will be the output of the following Python code snippet?
print('The sum of {0} and {1} is {2}'.format(2, 10, 12))
[A]The sum of 2 and 10 is 12
[B]Error
[C]The sum of 0 and 1 is 2
[D]None of the mentioned
Show Answer
Correct Answer: The sum of 2 and 10 is 12
Notes:
Answer: a
Explanation: The arguments passed to the function format can be integers also.
285). What will be the output of the following Python code snippet?
print('The sum of {0:b} and {1:x} is {2:o}'.format(2, 10, 12))
[A]The sum of 2 and 10 is 12
[B]The sum of 10 and a is 14
[C]The sum of 10 and a is c
[D]Error
Show Answer
Correct Answer: The sum of 10 and a is 14
Notes:
Answer: b
Explanation: 2 is converted to binary, 10 to hexadecimal and 12 to octal.
286). What will be the output of the following Python code snippet?
print('{:,}'.format(1112223334))
[A]1,112,223,334
[B]111,222,333,4
[C]1112223334
[D]Error
Show Answer
Correct Answer: 1,112,223,334
Notes:
Answer: a
Explanation: A comma is added after every third digit from the right.
287). What will be the output of the following Python code snippet?
print('{:,}'.format('1112223334'))
[A]1,112,223,334
[B]111,222,333,4
[C]1112223334
[D]Error
Show Answer
Correct Answer: Error
Notes:
Answer: d
Explanation: An integer is expected.
288). What will be the output of the following Python code snippet?
print('{:$}'.format(1112223334))
[A]1,112,223,334
[B]111,222,333,4
[C]1112223334
[D]Error
Show Answer
Correct Answer: Error
Notes:
Answer: d
Explanation: $ is an invalid format code.
289). . What will be the output of the following Python code snippet?
print('{:#}'.format(1112223334))
[A]1,112,223,334
[B]111,222,333,4
[C]1112223334
[D]Error
Show Answer
Correct Answer: 1112223334
Notes:
Answer: c
Explanation: The number is printed as it is.