Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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

i = 0
while i < 5:
    print(i)
    i += 1
    if i == 3:
        break
else:
    print(0)

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

View Answer

Answer: b
Explanation: The else part is not executed if control breaks out of the loop.




2 - Question

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

i = 0
while i < 3:
    print(i)
    i += 1
else:
    print(0)

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

View Answer

Answer: b
Explanation: The else part is executed when the condition in the while statement is false.




3 - Question

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

x = "abcdef"
while i in x:
    print(i, end=" ")

a) a b c d e f
b) abcdef
c) i i i i i i …
d) error

View Answer

Answer: d
Explanation: NameError, i is not defined.




4 - Question

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

x = "abcdef"
i = "i"
while i in x:
    print(i, end=" ")

a) no output
b) i i i i i i …
c) a b c d e f
d) abcdef

View Answer

Answer: a
Explanation: “i” is not in “abcdef”.




5 - Question

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

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

Answer: c
Explanation: As the value of i or x isn’t changing, the condition will always evaluate to True.




6 - Question

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

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

Answer: b
Explanation: Here i i i i i … printed continuously because as the value of i or x isn’t changing, the condition will always evaluate to True. But also here we use a citation marks on “i”, so, here i treated as a string, not like a variable.




7 - Question

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

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 mentioned

View Answer

Answer: b
Explanation: The string x is being shortened by one character in each iteration.




8 - Question

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

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 - Question

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

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 character in each iteration.




10 - Question

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

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:].

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners