Python Questions and Answers – While and For Loops – 5

This set of Python Questions and Answers for Experienced people focuses on “While and For Loops – 5”.

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

for i in range(2.0):
    print(i)

a) 0.0 1.0
b) 0 1
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: Object of type float cannot be interpreted as an integer.
advertisement
advertisement

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

for i in range(int(2.0)):
    print(i)

a) 0.0 1.0
b) 0 1
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: range(int(2.0)) is the same as range(2).
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
for i in range(float('inf')):
    print (i)

a) 0.0 0.1 0.2 0.3 …
b) 0 1 2 3 …
c) 0.0 1.0 2.0 3.0 …
d) none of the mentioned
View Answer

Answer: d
Explanation: Error, objects of type float cannot be interpreted as an integer.
advertisement

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

for i in range(int(float('inf'))):
    print (i)

a) 0.0 0.1 0.2 0.3 …
b) 0 1 2 3 …
c) 0.0 1.0 2.0 3.0 …
d) none of the mentioned
View Answer

Answer: d
Explanation: OverflowError, cannot convert float infinity to integer.

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

for i in [1, 2, 3, 4][::-1]:
    print(i, end=' ')

a) 1 2 3 4
b) 4 3 2 1
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: [::-1] reverses the list.

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

for i in ''.join(reversed(list('abcd'))):
    print (i)

a) a b c d
b) d c b a
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: ‘ ‘.join(reversed(list(‘abcd’))) reverses a string.

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

for i in 'abcd'[::-1]:
    print (i)

a) a b c d
b) d c b a
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: [::-1] reverses the string.

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

for i in '':
    print (i)

a) None
b) (nothing is printed)
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: The string does not have any character to loop over.

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

x = 2
for i in range(x):
    x += 1
    print (x)

a) 0 1 2 3 4 …
b) 0 1
c) 3 4
d) 0 1 2 3
View Answer

Answer: c
Explanation: Variable x is incremented and printed twice.

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

x = 2
for i in range(x):
    x -= 2
    print (x)

a) 0 1 2 3 4 …
b) 0 -2
c) 0
d) error
View Answer

Answer: b
Explanation: The loop is entered twice.

Sanfoundry Global Education & Learning Series – Python.

To practice all questions on Python for experienced people, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.