Home / Programming MCQs / JAVA MCQs / Question

M

Mr. Dubey • 51.17K Points
Coach

Q. What is the value of k after the following code fragment?~~~int k = 0;~~~int n = 12~~~while (k < n)~~~{~~~k = k + 1;~~~}

(A) 0.
(B) 11.
(C) 12.
(D) 10.
Explanation by: Monu Rathod
You might wonder that how this value will be 12. Since k is initialised with 0 and n with 12. 

Now while loop checks whether value of k is smaller than n or not. So this condition will be true unless te value of k becomes 11. 

Now the value of k is 11 and the condition is true to while loop will be executed and now k will become 12. 

So the final value of k will be 12. 

You must be Logged in to update hint/solution

Discusssion

Login to discuss.