Our Features

Our Features
.

Friday, 17 June 2016

Python Questions and Answers – While and For Loops – 5

This set of Python Questions & Answers focuses on “While and For Loops” and is useful for experienced people who are preparing for their interviews.
1. What is the output of the following?
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
2. What is the output of the following?
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
3. What is the output of the following?
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
4. What is the output of the following?
for i in range(int(float('inf'))):
    print (i)
advertisements
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
5. What is the output of the following?
for i in [1, 2, 3, 4][::-1]:
    print (i)
a) 1 2 3 4
b) 4 3 2 1
c) error
d) none of the mentioned
View Answer
6. What is the output of the following?
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
7. What is the output of the following?
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
advertisements
8. What is the output of the following?
for i in '':
    print (i)
a) None
b) (nothing is printed)
c) error
d) none of the mentioned
View Answer
9. What is the output of the following?
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
10. What is the output of the following?
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.

No comments:

Post a Comment