Our Features

Our Features
.

Sunday, 5 June 2016

Python Questions and Answers – While and For Loops – 2 Continued

5. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
    print(i, end = " ")
a) no output
b) i i i i i i …
c) a a a a a a …
d) a b c d e f
View Answer
6. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
    print('i', end = " ")
a) no output
b) i i i i i i …
c) a a a a a a …
d) a b c d e f
View Answer
7. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
    x = x[:-1]
    print(i, end = " ")
a) i i i i i i
b) a a a a a a
c) a a a a a
d) none of the mentiornedmentiorned
8. What is the output of the following?
x = "abcdef"
i = "a"
while i in x[:-1]:
    print(i, end = " ")
a) a a a a a
b) a a a a a a
c) a a a a a a …
d) a
View Answer
Answer: c
Explanation: String x is not being altered and i is in x[:-1].
9. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
    x = x[1:]
    print(i, end = " ")
a) a a a a a a
b) a
c) no output
d) error
View Answer
Answer: b
Explanation: The string x is being shortened by one charater in each iteration.
10. What is the output of the following?
x = "abcdef"
i = "a"
while i in x[1:]:
    print(i, end = " ")
a) a a a a a a
b) a
c) no output
d) error
View Answer
Answer: c
Explanation: i is not in x[1:].

No comments:

Post a Comment