Python Questions and Answers – While and For Loops – 4

This set of Python Questions and Answers for Freshers focuses on “While and For Loops – 4”.

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

x = 123
for i in x:
    print(i)

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

Answer: c
Explanation: Objects of type int are not iterable.
advertisement
advertisement

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

d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
    print(i)

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

Answer: a
Explanation: Loops over the keys of the dictionary.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
d = {0: 'a', 1: 'b', 2: 'c'}
for x, y in d:
    print(x, y)

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

Answer: d
Explanation: Error, objects of type int aren’t iterable.
advertisement

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

d = {0: 'a', 1: 'b', 2: 'c'}
for x, y in d.items():
    print(x, y)

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

Answer: c
Explanation: Loops over key, value pairs.

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

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.keys():
    print(d[x])

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

Answer: b
Explanation: Loops over the keys and prints the values.

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

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
    print(x)

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

Answer: b
Explanation: Loops over the values.

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

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
    print(d[x])

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

Answer: d
Explanation: Causes a KeyError.

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

d = {0, 1, 2}
for x in d.values():
    print(x)

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

Answer: c
Explanation: Objects of type set have no attribute values.

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

d = {0, 1, 2}
for x in d:
    print(x)

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

Answer: a
Explanation: Loops over the elements of the set and prints them.

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

d = {0, 1, 2}
for x in d:
    print(d.add(x))

a) 0 1 2
b) 0 1 2 0 1 2 0 1 2 …
c) None None None
d) None of the mentioned
View Answer

Answer: c
Explanation: Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.

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

for i in range(0):
    print(i)

a) 0
b) no output
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: range(0) is empty.

Sanfoundry Global Education & Learning Series – Python.

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