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

Download Printable PDF. VALID exam to help you PASS.

5 thoughts on “What is the result?

  1. 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. 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

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.