Test Series - python

Test Number 21/108

Q: 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
Solution: Object of type float cannot be interpreted as an integer.
Q: 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
Solution: range(int(2.0)) is the same as range(2).
Q: What will be the output of the following Python code snippet?

for i in [1, 2, 3, 4][::-1]:
    print (i)
A. 1 2 3 4
B. 0 1 2 3 …
C. error
D. 4 3 2 1
Solution: [::-1] reverses the list.
Q: 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
Solution: The string does not have any character to loop over.
Q: 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. 3 4
C. error
D. none of the mentioned
Solution: Variable x is incremented and printed twice.
Q: 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. error
D. 0
Solution: The loop is entered twice.

You Have Score    /6