250+ TOP MCQs on Python While and For Loops and Answers

Python Questions and Answers for Freshers on “While and For Loops”.

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
Answer: c
Clarification: Objects of type int are not iterable.

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
Answer: a
Clarification: Loops over the keys of the dictionary.

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

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
Answer: d
Clarification: Error, objects of type int aren’t iterable.

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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: 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

Answer: d
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: range(0) is empty.

questions on Python for freshers,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top