While and For Loops Questions and Answers

Python Questions and Answers – While and For Loops

This set of Advanced Python Questions & Answers focuses on “While and For Loops

1. 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

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

2. 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

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.

3. 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

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

4. 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

Explanation: String x is not being altered and i is in x[:-1].

5. 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

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

6. 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

Explanation: i is not in x[1:].

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

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

8. 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

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

9. 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

Explanation: NameError, i is not defined.

10. 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

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

The ‘for’ loop is only used when the number of iterations is already known. When the number of iterations is unknown, the ‘while’ loop is utilised. If the condition is not included in the ‘for’ loop, the loop will loop indefinitely. Compilation error occurs if the condition is not included in the ‘while’ loop. The for loop structure is written in a concise manner. Unlike a while loop, a for statement consumes the setup, condition, and increment/decrement on a single line, resulting in a looping structure that is shorter and easier to debug.

Leave a Reply

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