Nested Iteration

AP Computer Science A· difficulty 3/5

for (int i = 0; i < 3; i++) {
    for (int i = 0; i < 3; i++) {
        System.out.println("x");
    }
}

What is wrong?

  • A

    Compile-time inference fails

  • B

    Nothing is wrong

  • C

    Both loops share i and run nine times correctly

  • D

    Inner loop redeclares i; that variable is already in the enclosing scope

    check_circle

Explanation

Java disallows declaring a variable with the same name as one already in an enclosing block. Rename the inner variable, e.g., j.

Want 10 more like this — adaptive to your weak spots?

Related questions