Nested Iteration MCQ Practice

Question 1 of 10

A library clerk stamps a B for every bin on two shelves. What text does this Java code display?

for (int shelf = 0; shelf < 2; shelf++) {
    for (int book = 0; book < 4; book++) {
        System.out.print("B");
    }
}
  1. A. BBBB
  2. B. BB
  3. C. BBBBBBBB
  4. D. BBBBBBBBBBBB

Question 2 of 10

A pool clock reprints the wave number on every inner step. What text does this Java code display?

int wave = 1;
while (wave <= 3) {
    int step = 1;
    while (step <= 2) {
        System.out.print(wave);
        step++;
    }
    wave++;
}
  1. A. 123123
  2. B. 112233
  3. C. 123
  4. D. 111222333

Question 3 of 10

A garden trellis prints a decreasing outer label twice per row, with no line breaks. What text does this Java code display?

for (int r = 3; r >= 1; r--) {
    for (int c = 1; c <= 2; c++) {
        System.out.print(r);
    }
}
  1. A. 321321
  2. B. 333222111
  3. C. 123123
  4. D. 332211

Question 4 of 10

A kiln log counts firings in a growing inner loop. What text does this Java code display?

int n = 0;
for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= i; j++) {
        n++;
    }
}
System.out.print(n);
  1. A. 15
  2. B. 25
  3. C. 5
  4. D. 10

Question 5 of 10

A marching-band counter prints a countdown inside each outer pass. What text does this Java code display?

for (int x = 0; x < 3; x++) {
    for (int y = 2; y > 0; y--) {
        System.out.print(y);
    }
}
  1. A. 210210210
  2. B. 222111
  3. C. 212121
  4. D. 21

Question 6 of 10

A mailroom prints inner indices that start at 0 and stop at the current outer value. What text does this Java code display?

for (int x = 0; x <= 2; x++) {
    for (int y = 0; y <= x; y++) {
        System.out.print(y);
    }
}
  1. A. 012012012
  2. B. 001012
  3. C. 012
  4. D. 000111222

Question 7 of 10

A warehouse counts remaining slots from the current aisle to aisle 5. What text does this Java code display?

int sum = 0;
for (int a = 2; a <= 5; a++) {
    int b = a;
    while (b <= 5) {
        sum++;
        b++;
    }
}
System.out.print(sum);
  1. A. 4
  2. B. 16
  3. C. 10
  4. D. 8

Question 8 of 10

A badge printer marks stars first, then the outer index. What text does this Java code display?

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        System.out.print("*");
    }
    System.out.print(i);
}
  1. A. ***1***2***3
  2. B. 123***
  3. C. *1*2*3
  4. D. *********123

Question 9 of 10

A schedule board prints leftover hours, then a slash, including a pass whose inner loop does not run. What text does this Java code display?

for (int m = 1; m <= 4; m++) {
    for (int n = m; n < 4; n++) {
        System.out.print(n);
    }
    System.out.print("/");
}
  1. A. 123/234/34/4/
  2. B. 123/23/3/
  3. C. 1234/234/34/4/
  4. D. 123/23/3//

Question 10 of 10

A sensor log advances the inner index by 2 and stops before 4. What text does this Java code display?

int p = 1;
int count = 0;
while (p <= 4) {
    int q = p;
    while (q < 4) {
        count++;
        q += 2;
    }
    p++;
}
System.out.print(count);
  1. A. 6
  2. B. 4
  3. C. 8
  4. D. 3