Python MCQs on Formatting and Advance Formatting: SET 3

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

Check Answer

Answer: a
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’.

 

 

22. 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]’

Check Answer

Answer: b
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]’.

 

 

23. 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’

Check Answer

Answer: c
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’.

 

 

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

Check Answer
Answer: a
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’.

 

 

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

Check Answer

Answer: a
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.

 

 

26. 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’

Check Answer

Answer: a
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’].

 

 

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

Check Answer

Answer: c
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.

 

 

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

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

 

 

29. 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)

Check Answer

Answer: d
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).

 

 

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

Check Answer

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

 

Page  1  2  3  4

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment