hi, I might be wrong..if so please correct me. I feel the answer should be 42 i'e B. B'caz, when i=0 n will become 6 (n=n(n+1)= 1(1+1)=2) when i=1 n will become 6 (2(2+1) = 6) when i=2 ,the loop continues, when i=3, n becomes 42 (6(6+1)=42) when i=4 ,it doesn't go into loop. I hope I'm correct.If wrong please correct me. Thanks, Thiru
Hi All, Suneel is correct. The answer is C. You can test it with the following code:
It will run forever! Here's what happens: <pre> start: x=4; n=1; i=0; inside loop: i < x 0 < 4 result: yes i == 2 result: no i++ result: i = 1
n *= n + 1 1 * (1+1) result: n = 2 next iteration: i < x 1 < 4 result: yes i == 2 result: no i++ result: i = 2
n *= n + 1 2 * (2+1) result: n = 6 next iteration: i < x 2 < 4 result: yes i == 2 result: yes -> continue loop next iteration: i < x 2 < 4 result: yes i == 2 result: yes -> continue loop keeps going, and going and going .... </pre> Once i is equal to 2 ... it never changes; the 'continue' forces the JVM to skip the remainder of the loop and begin again from the start of while(). Hope that helps.
------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Hi All, The answer is : c).The method never returns due to an infinite loop. While i==2, the loop execute the 'continue' statement. That means 'i' never get changed, since the increment to 'i' is below the 'continue' statement. So i < X is always true. (X = 4).