Python MCQs on String: SET 8

71. 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

Check Answer

 

Answer: a
The arguments are accessed by their names.

 

 

72. 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

Check Answer
Answer: b
!r causes the characters ‘ or ” to be printed as well.

 

 

 

73. 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

Check Answer

Answer: c
IndexError, the tuple index is out of range.

 

 

74. 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

Check Answer

 

Answer: a
The elements of the tuple are accessed by their indices.

 

75. 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

Check Answer

Answer: a
The arguments passed to the function format can be integers also.

 

 

76. 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

Check Answer

Answer: b
2 is converted to binary, 10 to hexadecimal and 12 to octal.

 

 

77. 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

Check Answer

Answer: a
A comma is added after every third digit from the right.

 

 

78. 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

Check Answer
Answer: d
An integer is expected.

 

 

79. 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

Check Answer
Answer: d
$ is an invalid format code.

 

 

80. 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

Check Answer

Answer: c
The number is printed as it is.

 

Pages  1  2  3  4  5  6  7  8  9  10  11  12  13

 

 

 

 

Leave a Comment