This set of Python Multiple Choice Questions & Answers (MCQs) 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
Explanation: Object of type float cannot be interpreted as an integer.
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
Explanation: range(int(2.0)) is the same as range(2).
3. What will be the output of the following Python code?
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
Explanation: Error, objects of type float cannot be interpreted as an integer.
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
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
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
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
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
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
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
Explanation: The loop is entered twice.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Python Books
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Python Internship
- Apply for Programming Internship