Thankyou chandra for helping me with the study process and other queries as well.I have one question regarding the chisolm mock exam.See the code below: class JMM113 { public static void main (String[] args) { int i = 0, j = 0, k = 0; do while (i++ < 3) System.out.print(k++); while (j++ < 3); }}
What is the result of attempting to compile and run the program?
a. Prints: 0001 b. Prints: 012 c. Prints: 012012 d. Prints: 012345 e. Prints: 001122 f. Prints: 1112 g. Prints: 111222 h. Prints: 121212 i. Run-time error j. Compile-time error k. None of the above
The answer is Prints: 012 Can you please explain?
Nik Arora
Ranch Hand
Joined: Apr 26, 2007
Posts: 652
posted
0
HI,
public static void main (String[] args) { int i = 0, j = 0, k = 0; do while (i++ < 3) System.out.print(k++); while (j++ < 3); }}
i,j,k will be post increment so your are getting that output.
Regards NIK SCKP 1.5
John Stone
Ranch Hand
Joined: May 04, 2007
Posts: 332
posted
0
You can find helpful, if you debug this code using some IDE (netbeans, eclipse), so you can trace every line and see how each variable is changing.
rajesh baba
Greenhorn
Joined: May 19, 2007
Posts: 25
posted
0
class test { public static void main (String[] args) { int i = 0, j = 0, k = 0; do { while (i++ < 3 ) { System.out.print(k++); } } while (j++ < 3); } }
its a while loop inside do while loop so the code inside do while will be executed once so inner while loop prints 012 and do while runs three times but the value of i became 3 in so inner while condition fails so even though outer do while executes three time inner while loop executes only once so output is:012
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.