No Result
View All Result
Sunday, September 24, 2023
  • Login
Getting Placed
  • Home
  • Jobs
  • Exam
  • Admit Card
  • Result
  • Internship
  • Home
  • Jobs
  • Exam
  • Admit Card
  • Result
  • Internship
No Result
View All Result
Getting Placed
No Result
View All Result

Python MCQs on Formatting and Advance Formatting: SET 4

Abhishek by Abhishek
March 18, 2021
in Python
0
python

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

‘{a}{b}{a}’.format(a=’hello’, b=’world’)

a) ‘hello world’

b) ‘hello’ ‘world’ ‘hello’

c) ‘helloworldhello’

d) ‘hello’ ‘hello’ ‘world’

Check Answer
Answer: c
The code shown above prints the values substituted for a, b, a, in the same order. This operation is performed using the format function. Hence the output of the code is: ‘helloworldhello’.

 

 

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

D=dict(p=’py’, q=’thon’)

‘{p}{q}’.format(**D)

a) Error

b) python

c) py thon

d) {‘py’, ‘thon’}

Check Answer
Answer: b
The code shown above prints the values substituted for p and q in the same order. Note that there is no blank space between p and q. Hence the output is: sanfoundry.

 

 

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

‘The {} side {1} {2}’.format(‘bright’, ‘of’, ‘life’)

a) Error

b) ‘The bright side of life’

c) ‘The {bright} side {of} {life}’

d) No output

Check Answer

Answer: a
The code shown above results in an error. This is because we have switched from automatic field numbering to manual field numbering, that is, from {} to {1}. Hence this code results in an error.

 

 

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

‘{0:f}, {1:2f}, {2:05.2f}’.format(1.23456, 1.23456, 1.23456)

a) Error

b) ‘1.234560, 1.22345, 1.23’

c) No output

d) ‘1.234560, 1.234560, 01.23’

Check Answer
Answer: d
In the code shown above, various formatting options are displayed using the format option. Hence the output of this code is: ‘1.234560, 1.234560, 01.23’

 

 

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

‘%.2f%s’ % (1.2345, 99)

a) ‘1.2345’, ‘99’

b) ‘1.2399’

c) ‘1.234599’

d) 1.23, 99

Check Answer
Answer: b
In this code, we must notice that since multiple values haven been given, they should be enclosed in a tuple. Since the formatting format is %.2f, the value 1.2345 is reduced to two decimal places. Hence the output of the code shown above: ‘1.2399’.

 

 

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

‘%s’ %((1.23,),)

a) ‘(1.23,)’

b) 1.23,

c) (,1.23)

d) ‘1.23’

Check Answer

Answer: a
The formatting expression accepts either a single substitution value, or a tuple of one or more items. Since single item can be given either by itself or within the tuple, a tuple to be formatted must be provided as a tested tuple. Hence the output of the code is: >>> ‘%s’ %((1.23,),).

 

 

37. What will be the output of the following two codes?

i. ‘{0}’.format(4.56)

ii. ‘{0}’.format([4.56,])

a) ‘4.56’, ‘4.56,’

b) ‘4.56’, ‘[4.56]’

c) 4.56, [4.56,]

d) 4.56, [4.56,]

Check Answer
Answer: b
The code shown above shows the formatting option on the same value, that is 4.56, where in the second case, the value is enclosed in a list. Hence the output of the code shown above is:
‘4.56’, ‘[4.56]’

 

Page  1  2  3  4

Previous Post

Python MCQs on Tuple: SET 2

Next Post

Python MCQs on String: SET 7

Abhishek

Abhishek

Next Post
python

Python MCQs on String: SET 7

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow Us on Google News
No Result
View All Result

Recent Posts

  • Mustard Oil Price: सस्ता हुआ सरसों का तेल, खरीदने वालों की लगी भीड़
  • Bhojpuri Video: पवन सिंह ने रात के अंधेरे में मोनालिसा के बदन पर फेरा हाथ, मच गया बवाल
  • Gori Nagori ने उछल-उछल कर तोड़ दिया स्टेज, लगाए ताबड़तोड़ ठुमके
  • Haryanvi Dance: भरी जवानी में Gori Nagori के लगाए लटके-झटकें, देख बूढ़ों में आया जोश
  • Gori Nagori ने स्टेज पर उछल उछल कर लगाए जोरदार ठुमके, वायरल हुआ वीडियो

Recent Comments

  • Abhishek on Win 11 Download Links

Archives

  • September 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • August 2022
  • July 2022
  • October 2021
  • July 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • April 2020
  • February 2020

Categories

  • Admit Card
  • Algorithm and Programs
  • Exam
  • Internship
  • Java
  • Jobs
  • latest
  • politics and entertainment
  • Python
  • Result
  • Trademark
  • Uncategorized

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
  • Privacy Policy
  • DMCA
  • Terms of Use
  • Sitemap

© 2020 Getting Placed

No Result
View All Result
  • Home
  • Jobs
  • Exam
  • Admit Card
  • Result
  • Internship

© 2020 Getting Placed

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In