Thursday , September 19 2024

How many stars (*) will the following snippet send to the console? i = 0 while i < i + 2 : i += 1 print("*") else: print("*")

Questions BankCategory: Python Essentials 1How many stars (*) will the following snippet send to the console? i = 0 while i < i + 2 : i += 1 print("*") else: print("*")
How many stars (*) will the following snippet send to the console?

i = 0
while i < i + 2 :
    i += 1
    print("*")
else:
    print("*")
  • two
  • one
  • zero
  • the snippet will enter an infinite loop, printing one star per line

Explanation: Because i = 0, the result of the following comparison: i < 1 +2 is True, so the program enters the while loop. However, in the body of the loop i is then incremented, not decremented, which means the result of the comparison i < 1 +2 will always evaluate to True, which means the loop will perform an infinite number of iterations, executing the print(“*”) function with each and every iteration.

More Questions: Python Essentials 1 (PE1) Course Final Test