Consider the following pseudocode:
x:=1;
i:=1;
while (x ≤ 500)
begin
x := 2^x ;
i:=i+1;
end
What is the value of i at the end of the pseudocode?
Answer: B
Correct answer: 5
Each pass replaces x by \(2^x\), which grows extremely fast:
| after pass | - | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| x | 1 | 2 | 4 | 16 | 65536 |
| i | 1 | 2 | 3 | 4 | 5 |
The test \(x \le 500\) fails once x reaches 65536, and i is then 5.
Please log in or register to report this question.