Open In App

Capgemini | Pseudocode Questions | Question 18

Predict the output of the following pseudocode if p=4 and q=2:

Integer solve (Integer p, Integer q)
Integer value
while(q)
  value = p MOD q
  p = q
  q = value
End while 
return p
End function solve()

(A)



1

(B)



2

(C)

3

(D)

5

Answer: (B)
Explanation:

do while loop runs till q is not zero 
In each iteration 
1) value is updated with a value p MOD q 
2) p is assigned the value of q 
3) q is assigned value of variable value 
the value of p, q changes in the order:4, 2 -> 2, 0, and the loop terminates 

At last value p i.e 2 is returned.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :
Uncategorized