Consider the following pseudo-code x:=1; i:=1; while (x <= 1000) begin x:=2^x;…
2007
Consider the following pseudo-code
x:=1;
i:=1;
while (x <= 1000)
begin
x:=2^x;
i:=i+1;
end;What is the value of i at the end of the pseudo-code?
- A.
4
- B.
5
- C.
6
- D.
7
Attempted by 139 students.
Show answer & explanation
Correct answer: B
The pseudo-code initializes x = 1 and i = 1.
Iteration 1: Condition (1 ≤ 1000) is true. x becomes 2^1 = 2, i increments to 2.
Iteration 2: Condition (2 ≤ 1000) is true. x becomes 2^2 = 4, i increments to 3.
Iteration 3: Condition (4 ≤ 1000) is true. x becomes 2^4 = 16, i increments to 4.
Iteration 4: Condition (16 ≤ 1000) is true. x becomes 2^16 = 65536, i increments to 5.
Iteration 5: Condition (65536 ≤ 1000) is false. The loop terminates.
The final value of i is 5.