# What is the result?

Given the code fragment:

What is the result?
A. 2 4
B. 0 2 4 6
C. 0 2 4
D. Compilation fails

## 5 thoughts on “What is the result?”

1. Dni says:

Option C.

public class Test {

public static void main(String[] args) {

int ii = 0;
int jj = 7;

for (ii = 0; ii < jj – 1; ii = ii +2) {
System.out.print(ii + " ");
}
}
}

Output:
0 2 4

Explanation:
the loop will break after the 3rd run, because "ii < jj – 1" which is " ii < 6"!

2. Deniz says:

Because variable ii is already defined in method main

3. Manohar says:

it won’t print 6 as ii<6 it will come out of the loop

4. M says:

No the answer is B. Iterate until ii is no longer smaller than jj, so when I is 7 or higher. At the end of one iteration, add 2 to ii. This is after the print statement.

So it prints 0 , increments to 2
Then it prints 2 , increments to 4
Then it prints 4 , increments to 6
Then it prints 6 , increments to 8
Then it is no longer smaller than 7, so the loop ends.

The result is “0 2 4 6 “

1
12