The Gateway to Computer Science Excellence
+8 votes
2.4k views

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?

  1. 4
  2. 5
  3. 6
  4. 7
in Programming by Veteran (105k points) | 2.4k views
–1
Is it A.4
+1
Answer be 5

I=1         n=1

2                 2

 3                  2^2

4                      2^4

5                        2^16

Than loop terminate means i=5

3 Answers

+10 votes
Best answer
1st time ... x = 2 and i = 2

2nd time ...x=4 and i=3

3rd time.....x=16 and i = 4

4th time ......before entering the loop value of x < 1000 ....it is 16 so it will enter the loop

and make i = 5

5th time x>1000 so while loop doesn't execute.....

SO the answer is i = 5
by Active (1k points)
selected by
0
4th time condition is false why..??

3rd tym also it is false na...? As 16 <1000..

????
0
4th time x becomes 2^16 becomes after the condition check.

So in 5th time the condition fails.( 2^16<1000)

Hence i=5.
0

what does "

while (x $\leq$ 1000)

means..??

0
@ kashyap avi      ...no value of i will be 5 check again
0
@dhairya ...while (x<=1000)
0
thank you ... it was my mistake
0
thanxx guys... I got it now...:D
+4 votes
initialy 1st itr.. 1<=1000(true) 2nd itr..  2<=1000(true) 3rd itrn  ..4<=1000(true) 4th itrn   16<=1000(true)
x=1 x=2 x=4 x=16 x=>1000
i=1 i=2 i=3 i=4 i=5

then in the next iteration while loop will be false

and value of i=5.

B is the ans

by Boss (11.1k points)
+1 vote
Answer is Option B .
by (21 points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,385 answers
198,557 comments
105,369 users