MCQ Single Best Answer Moderate

QHow many times will the following while loop execute?

i = 0
while i < 5:
    print(i)
    i += 1

ID: #23082 While Loop in Programming Language 83 views
Question Info
#23082Q ID
ModerateDifficulty
While Loop in Programming LanguageTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A 1
  • B 5
  • C 4
  • D Infinite
Correct Answer: Option B

Explanation

The while loop starts with i = 0 and checks if the condition i < 5 is true. Since i is initially 0, it enters the loop and prints the value of i. After printing, the statement i += 1 increments i by 1. This process repeats until i reaches 5, at which point the condition i < 5 becomes false, and the loop terminates. Therefore, the loop executes 5 times, printing the values 0, 1, 2, 3, and 4. Option A is incorrect because it would only run once if the condition was set differently, and Option D would only be correct if there was no increment, which would result in an infinite loop.

Share This Question

Challenge a friend or share with your study group.