InterviewSolution
Saved Bookmarks
| 1. |
Give the output of the following program segment and also mention the number of times the loop is executed:int a,b; for (a=6, b=4; a< =24; a=a + 6) { if (a%b= =0) break; } System, out .println(a); |
|
Answer» Output is 12. Twice. In the loop : for (a = 6, b=4; a< =24; a = a+6), the value of a will be incrementing as 6, 24 and upon incrementing the value to 42, the loop will terminate. Accordingly the loop has to execute two times. But within the loop there is a condition : if(a%b = =0) break; This means when remainder on dividing a by b comes out to be 0 (at 24/4 = 0), the condition breaks and from the above it is clear that value of a is incremented from 6 to 24 when the loop executes second time. |
|