Can anyone explain why the answers are what they are please? The first one i guess its because the stopping condition is already reached so it skips the statement but question 13 why would it not print 4321?
12.What output is produced by the following code:
int n;
for (n = 1; n > 4; n++)
System.out.print(n);
a) 12345
b) 1234
c) 0 —> for loop never runs because stopping condition already met.
d) It produces no output*
13.What output is produced by the following code:
int n;
for (n = 4; n > 0; n--);
System.out.print(n);
a) 43210
b) 4321
c) 0* —> semi colon after for loop, causing print statement to run after loop is finished
d) It produces no output
Regarding question 13: there’s a semicolon behind the for-loop. Because of that, the loop will first be execuetd until n no longer is > 0 => it is zero then. Then, the print will show a zero.