If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2;


If x is an int where x = 1, what will x be after the following loop terminates?


while (x < 100)

x *= 2;

a) 2

b) 64

c) 100

d) 128

e) None of the above, this is an infinite loop

Answer: d. Explanation: With x = 1, the loop condition is true and the statement executes, doubling x, which is now 2. Since the loop condition is still true, the statement executes again, doubling x to 4. The condition remains true for the next 4 iterations, where x becomes 8, 16, 32 and 64. Since (x < 100) is still true when x = 64, the loop iterates again and x becomes 2 * 64 = 128. Now, (x < 100) is no longer true and the loop terminates with x = 128.