Python Questions and Answers – While and For Loops – 6

This set of Python Technical Questions & Answers focuses on “While and For Loops – 6”.

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

for i in range(10):
    if i == 5:
        break
    else:
        print(i)
else:
    print("Here")

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

Answer: c
Explanation: The else part is executed if control doesn’t break out of the loop.
advertisement
advertisement

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

for i in range(5):
    if i == 5:
        break
    else:
        print(i)
else:
    print("Here")

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

Answer: a
Explanation: The else part is executed if control doesn’t break out of the loop.

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

advertisement
x = (i for i in range(3))
for i in x:
    print(i)

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

Answer: a
Explanation: The first statement creates a generator object.
advertisement

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

x = (i for i in range(3))
for i in x:
    print(i)
for i in x:
    print(i)

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

Answer: a
Explanation: We can loop over a generator object only once.

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

string = "my name is x"
for i in string:
    print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer

Answer: a
Explanation: Variable i takes the value of one character at a time.

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

string = "my name is x"
for i in string.split():
    print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer

Answer: c
Explanation: Variable i takes the value of one word at a time.

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

a = [0, 1, 2, 3]
for a[-1] in a:
    print(a[-1])

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

Answer: b
Explanation: The value of a[-1] changes in each iteration.

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

a = [0, 1, 2, 3]
for a[0] in a:
    print(a[0])

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

Answer: a
Explanation: The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.

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

a = [0, 1, 2, 3]
i = -2
for i not in a:
    print(i)
    i += 1

a) -2 -1
b) 0
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: SyntaxError, not in isn’t allowed in for loops.

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

string = "my name is x"
for i in ' '.join(string.split()):
    print (i, end=", ")

a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer

Answer: a
Explanation: Variable i takes the value of one character at a time.

Sanfoundry Global Education & Learning Series – Python.

To practice all technical questions on Python, 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.