for Loops MCQ Practice

Question 1 of 10

A ticket printer uses this loop. Which header part is the Boolean expression tested before each body execution?

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

Question 2 of 10

A clerk totals booth numbers. What text does this Java code display?

int total = 0;
for (int n = 2; n <= 5; n++) {
    total += n;
}
System.out.print(total);
  1. A. 14
  2. B. 9
  3. C. 20
  4. D. 7

Question 3 of 10

A lane counter prints its loop control variable. What text does this Java code display?

for (int k = 0; k < 3; k++) {
    System.out.print(k);
}
  1. A. 012
  2. B. 0123
  3. C. 123
  4. D. 01

Question 4 of 10

A running total is printed inside the body. What text does this Java code display?

int show = 0;
for (int i = 1; i < 4; i++) {
    show += i;
    System.out.print(show);
}
  1. A. 136
  2. B. 123
  3. C. 16
  4. D. 149

Question 5 of 10

Which while loop is equivalent to this for loop?

for (int i = 3; i < 9; i += 2) {
    System.out.print(i);
}
  1. A. int i = 3; while (i < 9) { System.out.print(i); i += 2; }
  2. B. int i = 3; while (i < 9) { i += 2; System.out.print(i); }
  3. C. int i = 3; while (i <= 9) { System.out.print(i); i += 2; }
  4. D. int i = 3; while (i < 9) { System.out.print(i); i++; }

Question 6 of 10

The loop control variable is declared outside the for header. What text does this Java code display?

int n = 8;
for (n = 8; n > 0; n -= 3) {
    System.out.print(n);
}
System.out.print(":" + n);
  1. A. 852:-1
  2. B. 852:2
  3. C. 85:-1
  4. D. 852:0

Question 7 of 10

A loop control variable lives past the loop. What text does this Java code display?

int i;
for (i = 2; i <= 5; i++) {
    System.out.print(i);
}
System.out.print(i);
  1. A. 23456
  2. B. 2345
  3. C. 23455
  4. D. 12345

Question 8 of 10

A starter value is doubled in a counted loop. What text does this Java code display?

int x = 1;
for (int c = 0; c < 4; c++) {
    x *= 2;
}
System.out.print(x);
  1. A. 16
  2. B. 8
  3. C. 32
  4. D. 4

Question 9 of 10

A spare-parts counter uses this loop. What text does this Java code display?

int t = 7;
for (int i = 7; i < 7; i++) {
    t++;
}
System.out.print(t);
  1. A. 7
  2. B. 8
  3. C. 0
  4. D. 14

Question 10 of 10

Hits are counted with a step of 2. What text does this Java code display?

int hits = 0;
int start = 3;
for (int i = start; i <= start + 4; i += 2) {
    hits++;
}
System.out.print(hits);
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 5