Contents

What will be the output of the following Python expression if the value of x is 34?
print(“%f”%x)
a) 34.00                                
b) 34.0000
c) 34.000000                       
d) 34.00000000

c

Explanation: The expression shown above normally returns the value with 6 decimal points if it is not specified with any number. Hence the output of this expression will be: 34.000000 (6 decimal points).


What will be the output of the following Python expression if x=56.236?
print("%.2f"%x)
a) 56.00                                
b) 56.24
c) 56.23                                
d) 0056.236

b

Explanation: The expression shown above rounds off the given number to the number of decimal places specified. Since the expression given specifies rounding off to two decimal places, the output of this expression will be 56.24. Had the value been x=56.234 (last digit being any number less than 5), the output would have been 56.23.


What will be the output of the following Python expression if x=22.19?
print("%5.2f"%x)
a) 22.1900                           
b) 22.00000
c) 22.19                                
d) 22.20

c

Explanation: The output of the expression above will be 22.19. This expression specifies that the total number of digits (including the decimal point) should be 5, rounded off to two decimal places.


The expression shown below results in an error.
print("-%5d0",989)
a) True                                  
b) False

b

Explanation: The expression shown above does not result in an error. The output of this expression is -%5d0 989. Hence this statement is incorrect.


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

c

Explanation: 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.


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

b

Explanation: 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.