Consider the following pseudocode: x :=1; i :=1; while (x < 500) begin x :=2x…
2011
Consider the following pseudocode:
x :=1;
i :=1;
while (x < 500)
begin
x :=2x ;
i :=i+1;
end
What is the value of i at the end of the pseudocode?
- A.
4
- B.
5
- C.
6
- D.
7
Attempted by 491 students.
Show answer & explanation
Correct answer: B
Initially: x = 1, i = 1
Iterations:
x = 2¹ = 2 → i = 2
x = 2² = 4 → i = 3
x = 2⁴ = 16 → i = 4
x = 2¹⁶ = 65536 → i = 5
Now x > 500, so loop stops.
Final value of i = 5