3,132 views
20 votes
20 votes

Consider the following psuedocode fragment, where $y$ is an integer that has been initialized.

int i=1
int j=1
while (i<10):
    j=j*i
    i=i+1
    if (i==y):
        break
    end if
end while

Consider the following statements:

  1. $(i==10)$ or $(i==y)$
  2. If $y > 10$, then $i==10$
  3. If $j=6$, then $y==4$

Which of the above statements is/are TRUE at the end of the while loop? Choose from the following options.

  1. i only
  2. iii only
  3. ii and iii only
  4. i, ii, and iii
  5. None of the above

5 Answers

Best answer
23 votes
23 votes

Consider the situations when loop gets broken -

  1. If $i$ becomes $10$, or $i$ becomes equal to $y$. $\Longrightarrow$ (i) is correct.
  2. If $y > 10$, the control will come out of the loop when $i$ becomes $10$. $\Longrightarrow$ (ii) is correct.
  3. During $1$st iteration, $j =1, i =2$
    • $2$nd iteration, $j = 2, i =3$
    • $3$rd iteration, $j = 6, i =4$

$4^{th}$ iteration must not occur because $j$ will become $24$. Hence during $3^{rd}$ iteration break statement must have been executed and hence, $y$ must be equal to $4$ (equal to $i$ during the $3^{rd}$ iteration).

This implies (iii) is also correct.

Hence, Option D is correct.

edited by
1 votes
1 votes

while loop will end in cases:
1) if it satisfy if condition OR 
2) i=10

i.  CORRECT as it satisfy above 2 cases 
ii.  CORRECT as If y>10 means it not satisfy if condition so to end the whle loop it should be i==10
iii.  CORRECT check  tarun_svbk 's explanation 

Hence Ans is D 

Answer:

Related questions

12 votes
12 votes
2 answers
1
36 votes
36 votes
6 answers
4