Test Series - python

Test Number 18/108

Q: 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
Solution: The else part is not executed if control breaks out of the loop.
Q: 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
Solution: The else part is executed when the condition in the while statement is false.
Q: 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
Solution: NameError, i is not defined.
Q: 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
Solution: “i” is not in “abcdef”.
Q: 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
Solution: As the value of i or x isn’t changing, the condition will always evaluate to True.
Q: 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
Solution: The string x is being shortened by one character in each iteration.
Q: 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
Solution: String x is not being altered and i is in x[:-1].
Q: 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
Solution: The string x is being shortened by one character in each iteration.
Q: 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
Solution: i is not in x[1:].

You Have Score    /9