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)
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)
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=" ")
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=" ")
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 = " ")
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 = " ")
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 = " ")
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 = " ")
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 = " ")
Solution: i is not in x[1:].
You Have Score    | /9 |