Thursday , September 19 2024

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

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

i = 0
while i <= 3 :
    i += 2
    print("*")
  • zero
  • one
  • three
  • two

Explanation: Let’s analyze this example:

  • the i variable is assigned the integer value of 0,
  • the while loop compares if 0 <= 3, and since it is True, the loop is entered,
  • the i variable is incremented by 2, and a first star is printed on the console,
  • the while loop now compares if 2 <= 3, and since it is True, the loop is entered,
  • the variable i is incremented by 2, and a second star is printed on the console,
  • the while loop now compares if 4 <= 3, and since it is False, the loop is no longer executed.

More Questions: Python Essentials 1 – Module 3 Test