A web application is not responding to user input. The developer checks the code and finds an infinite loop. Which change will fix the infinite loop?
A counter limits the number of iterations, ensuring the loop eventually ends.
Why this answer
Option C is correct because an infinite loop occurs when the loop's termination condition is never met. By adding a counter variable that increments with each iteration and checking it in the condition, the loop will eventually exit when the counter reaches a specified limit, thus breaking the infinite loop.
Exam trap
The trap here is that candidates may think changing the loop type (e.g., for to while) or removing the body will fix the infinite loop, but the core issue is the lack of a proper termination condition, which only a counter or similar mechanism can resolve.
How to eliminate wrong answers
Option A is wrong because removing the loop body does not change the loop's condition; the loop will still run infinitely if the condition never becomes false. Option B is wrong because adding a break statement will exit the loop immediately, but it does not address the root cause of the infinite loop and may lead to premature termination or logic errors. Option D is wrong because changing from a for loop to a while loop does not inherently fix an infinite loop; the condition must still be properly defined to allow termination.