Python MCQs on Formatting and Advance Formatting: SET 2

11. What will be the output of the following Python code snippet?

‘%d %s %g you’ %(1, ‘hello’, 4.0)

a) Error

b) 1 hello you 4.0

c) 1 hello 4 you

d) 1 4 hello you

Check Answer
Answer: c
In the snippet of code shown above, three values are inserted into the target string. When we insert more than one value, we should group the values on the right in a tuple. The % formatting expression operator expects either a single item or a tuple of one or more items on its right side.

 

 

12. The output of which of the codes shown below will be: “There are 4 blue birds.”?

a) ‘There are %g %d birds.’ %4 %blue

b) ‘There are %d %s birds.’ %(4, blue)

c) ‘There are %s %d birds.’ %[4, blue]

d) ‘There are %d %s birds.’ 4, blue

Check Answer
Answer: b
The code ‘There are %d %s birds.’ %(4, blue) results in the output: There are 4 blue birds. When we insert more than one value, we should group the values on the right in a tuple.

 

 

13. What will be the output of the python code shown below for various styles of format specifiers?

x=1234

res=’integers:…%d…%-6d…%06d’ %(x, x, x)

res

a) ‘integers:…1234…1234 …001234’

b) ‘integers…1234…1234…123400’

c) ‘integers:… 1234…1234…001234’

d) ‘integers:…1234…1234…001234’

Check Answer
Answer: a
The code shown above prints 1234 for the format specified %d, ‘1234 ’ for the format specifier %-6d (minus ‘-‘ sign signifies left justification), and 001234 for the format specifier %06d. Hence the output of this code is: ‘integers:…1234…1234 …001234’

 

 

14. What will be the output of the following Python code snippet?

x=3.3456789

‘%f | %e | %g’ %(x, x, x)

a) Error

b) ‘3.3456789 | 3.3456789+00 | 3.345678’

c) ‘3.345678 | 3.345678e+0 | 3.345678’

d) ‘3.345679 | 3.345679e+00 | 3.34568’

Check Answer
Answer: d
The %f %e and %g format specifiers represent floating point numbers in different ways. %e and %E are the same, except that the exponent is in lowercase. %g chooses the format by number content. Hence the output of this code is: ‘3.345679 | 3.345679e+00 | 3.34568’.

 

 

15. What will be the output of the following Python code snippet?

x=3.3456789

‘%-6.2f | %05.2f | %+06.1f’ %(x, x, x)

a) ‘3.35 | 03.35 | +003.3’

b) ‘3.3456789 | 03.3456789 | +03.3456789’

c) Error

d) ‘3.34 | 03.34 | 03.34+’

Check Answer
Answer: a
The code shown above rounds the floating point value to two decimal places. In this code, a variety of addition formatting features such as zero padding, total field width etc. Hence the output of this code is: ‘3.35 | 03.35 | +003.3’.

 

 

16. What will be the output of the following Python code snippet?

x=3.3456789

‘%s’ %x, str(x)

a) Error

b) (‘3.3456789’, ‘3.3456789’)

c) (3.3456789, 3.3456789)

d) (‘3.3456789’, 3.3456789)

Check Answer
Answer: b
We can simply convert strings with a %s format expression or the str built-in function. Both of these methods have been shown in this code. Hence the output is: ) (‘3.3456789’, ‘3.3456789’)

 

 

17. What will be the output of the following Python code snippet?

‘%(qty)d more %(food)s’ %{‘qty’:1, ‘food’: ‘spam’}

a) Error

b) No output

c) ‘1 more foods’

d) ‘1 more spam’

Check Answer

Answer: d
String formatting also allows conversion targets on the left to refer to the keys in a dictionary coded on the right and fetch the corresponding values. In the code shown above, (qty) and (food) in the format string on the left refers to keys in the dictionary literal on the right and fetch their assorted values. Hence the output of the code shown above is: 1 more spam.

 

 

18. What will be the output of the following Python code snippet?

a=’hello’

q=10

vars()

a) {‘a’ : ‘hello’, ‘q’ : 10, ……..plus built-in names set by Python….}

b) {……Built in names set by Python……}

c) {‘a’ : ‘hello’, ‘q’ : 10}

d) Error

Check Answer

 

Answer: a
The built in function vars() returns a dictionary containing all the variables that exist in the place. Hence the output of the code shown above is: {‘a’ : ‘hello’, ‘q’ : 10, ……..plus built-in names set by Python….}

 

19. What will be the output of the following Python code?

s='{0}, {1}, and {2}’

s.format(‘hello’, ‘good’, ‘morning’)

a) ‘hello good and morning’

b) ‘hello, good, morning’

c) ‘hello, good, and morning’

d) Error

Check Answer
Answer: c
Within the subject string, curly braces designate substitution targets and arguments to be inserted either by position or keyword. Hence the output of the code shown above:’hello, good,and morning’.

 

 

20. What will be the output of the following Python code?

s=’%s, %s & %s’

s%(‘mumbai’, ‘kolkata’, ‘delhi’)

a) mumbai kolkata & delhi

b) Error

c) No output

d) ‘mumbai, kolkata & delhi’

Check Answer

Answer: d
In the code shown above, the format specifier %s is replaced by the designated substitution. Hence the output of the code shown above is: ‘mumbai, kolkata & delhi’.

 

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