Skip to main content

Java Topic Practice

Nested Loops

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

For practice use only.

Nested Loops MCQ Practice

Nested Loops Java practice with 15 questions, code tracing, and a worked explanation for every answer.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - Nested Loops: track the final printed value.

int count = 0;
for (int i = 1; i <= 5; i++) {
  for (int j = i; j <= 5; j++) {
    count++;
  }
}
System.out.print(count);

All 15 nested loops questions

Work through the interactive quiz above first. This is the full nested loops question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - Nested Loops: track the final printed value.

    int count = 0;
    for (int i = 1; i <= 5; i++) {
      for (int j = i; j <= 5; j++) {
        count++;
      }
    }
    System.out.print(count);
    • 25
    • 10
    • 20
    • 15
    Show worked solution

    Correct answer: 15

    The inner loop starts at i rather than 1, so each pass is shorter than the last and only the upper triangle is visited.

  2. 2.Code Tracing 12 - Nested Loops: follow the variable updates.

    int total = 26;
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        if (j > i) break;
        total += j;
      }
    }
    System.out.print(total);
    • 61
    • 65
    • 40
    • 60
    Show worked solution

    Correct answer: 61

    The break stops only the inner loop, so each outer pass adds 1 through i and then moves on.

  3. 3.Code Tracing 28 - Nested Loops: trace the branch and loop path.

    int total = 38;
    for (int i = 1; i <= 4; i++) {
      for (int j = 1; j <= 5; j++) {
        if (j % 2 == 0) continue;
        total += i * j;
      }
    }
    System.out.print(total);
    • 128
    • 134
    • 60
    • 125
    Show worked solution

    Correct answer: 128

    continue skips only the rest of the current inner pass, so even j values contribute nothing but the loop keeps running.

  4. 4.Code Tracing 39 - Nested Loops: watch the index changes.

    String out = "ARRA";
    for (int i = 1; i <= 3; i++) {
      for (int j = 0; j < i; j++) {
        out += i;
      }
      out += "-";
    }
    System.out.print(out);
    • ARRA1-22-333--
    • ARRA1-22-333-
    • ARRA122333
    • 1-2-3-
    Show worked solution

    Correct answer: ARRA1-22-333-

    The dash is appended by the outer loop, so it appears once per outer pass rather than once per character.

  5. 5.Code Tracing 50 - Nested Loops: evaluate the state change step by step.

    int found = -1;
    outer:
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        if (i * j == 10) {
          found = i * 19 + j;
          break outer;
        }
      }
    }
    System.out.print(found);
    • 44
    • 10
    • 43
    • -1
    Show worked solution

    Correct answer: 43

    A labeled break exits both loops at once, so the first matching pair found is the one reported.

  6. 6.What is printed by the following code segment?

    int c = 0;
    for (int i = 1; i <= 4; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            for (int k = 1; k <= j; k++)
            {
                c++;
            }
        }
    }
    System.out.print(c);
    • 10
    • 20
    • 24
    • 16
    Show worked solution

    Correct answer: 20

    The innermost count grows as 1, 3, 6, and 10 across the outer passes.

  7. 7.What is printed by the following code segment?

    String out = "";
    for (int i = 1; i <= 3; i++)
    {
        for (int j = 3; j >= i; j--)
        {
            out += j;
        }
        out += "|";
    }
    System.out.print(out);
    • 123|23|3|
    • 321|321|321|
    • 321|32|3|
    • 3|32|321|
    Show worked solution

    Correct answer: 321|32|3|

    Each row counts down from 3 to the current outer value, so the rows shrink.

  8. 8.What is printed by the following code segment?

    int t = 0;
    for (int i = 1; i <= 3; i++)
    {
        for (int j = 1; j <= 3; j++)
        {
            if (i == j) { continue; }
            if (i > j) { break; }
            t += i * j;
        }
    }
    System.out.print(t);
    • 11
    • 6
    • 9
    • 5
    Show worked solution

    Correct answer: 5

    When i is 1 the row adds 2 and 3. Every later row breaks on its first pass because the inner index starts behind, so nothing more accumulates.

  9. 9.What is printed by the following code segment?

    int c = 0;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            if ((i + j) % 2 == 0) { c++; }
        }
    }
    System.out.print(c);
    • 13
    • 12
    • 25
    • 10
    Show worked solution

    Correct answer: 13

    In a five by five grid the cells whose coordinates share a parity form the larger of the two checkerboard colors.

  10. 10.What is printed by the following code segment?

    int s = 0;
    for (int i = 1; i <= 3; i++)
    {
        int inner = 0;
        for (int j = 1; j <= 3; j++)
        {
            inner += j;
        }
        s += inner * i;
    }
    System.out.print(s);
    • 18
    • 36
    • 54
    • 24
    Show worked solution

    Correct answer: 36

    The inner total is 6 every pass, and multiplying by 1, 2, and 3 gives 6, 12, and 18.

  11. 11.What is printed by the following code segment?

    String out = "";
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            out += i == j ? "X" : ".";
        }
    }
    System.out.print(out);
    • X..X..X..
    • .X..X..X.
    • X...X...X
    • XXX......
    Show worked solution

    Correct answer: X...X...X

    Nine characters are printed and only the three diagonal positions are marked.

  12. 12.What is printed by the following code segment?

    int c = 0;
    for (int i = 2; i <= 10; i++)
    {
        boolean p = true;
        for (int d = 2; d < i; d++)
        {
            if (i % d == 0) { p = false; }
        }
        if (p) { c++; }
    }
    System.out.print(c);
    • 5
    • 3
    • 9
    • 4
    Show worked solution

    Correct answer: 4

    The primes from 2 through 10 are 2, 3, 5, and 7.

  13. 13.What is printed by the following code segment?

    int t = 0;
    for (int i = 1; i <= 3; i++)
    {
        for (int j = i; j <= 3; j++)
        {
            for (int k = j; k <= 3; k++)
            {
                t++;
            }
        }
    }
    System.out.print(t);
    • 10
    • 27
    • 6
    • 14
    Show worked solution

    Correct answer: 10

    This counts the non-decreasing triples drawn from three values.

  14. 14.What is printed by the following code segment?

    String out = "";
    for (int r = 1; r <= 3; r++)
    {
        for (int c = 1; c <= 3; c++)
        {
            if (c % r == 0) { out += c; }
        }
    }
    System.out.print(out);
    • 123
    • 12323
    • 1232
    • 1233
    Show worked solution

    Correct answer: 12323

    Row 1 keeps every column, row 2 keeps only column 2, and row 3 keeps only column 3.

  15. 15.What is printed by the following code segment?

    int c = 0;
    outer:
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            if (i * j > 4) { break outer; }
            c++;
        }
    }
    System.out.print(c);
    • 16
    • 9
    • 11
    • 12
    Show worked solution

    Correct answer: 11

    A labeled break exits both loops at once, which happens when the product first exceeds 4.