for Loops MCQ Practice

Question 1 of 10

In this header, which part executes only once?

for (int step = 0; step < 6; step++) {
    System.out.print(step);
}
  1. A. int step = 0
  2. B. step < 6
  3. C. step++
  4. D. the loop body

Question 2 of 10

A descending counter feeds a sum. What text does this Java code display?

int sum = 0;
for (int d = 3; d >= 1; d--) {
    sum += d;
}
System.out.print(sum);
  1. A. 6
  2. B. 5
  3. C. 3
  4. D. 0

Question 3 of 10

Even wagon IDs are printed. What text does this Java code display?

for (int w = 2; w < 8; w += 2) {
    System.out.print(w);
}
  1. A. 246
  2. B. 2468
  3. C. 234567
  4. D. 24

Question 4 of 10

An accumulator is printed after each add. What text does this Java code display?

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

Question 5 of 10

Which while loop is equivalent to this for loop?

for (int k = 5; k > 1; k--) {
    System.out.print(k);
}
  1. A. int k = 5; while (k > 1) { System.out.print(k); k--; }
  2. B. int k = 5; while (k >= 1) { System.out.print(k); k--; }
  3. C. int k = 5; while (k > 1) { k--; System.out.print(k); }
  4. D. int k = 5; while (k > 1) { System.out.print(k); k++; }

Question 6 of 10

A growth log prints the loop control variable, then its value after the loop. What text does this Java code display?

int n;
for (n = 1; n < 20; n *= 2) {
    System.out.print(n);
}
System.out.print("|" + n);
  1. A. 124816|32
  2. B. 124816|16
  3. C. 1248|16
  4. D. 12481632|32

Question 7 of 10

A crate counter decreases once per counted trip. What text does this Java code display?

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

Question 8 of 10

A gear ratio doubles by adding the variable to itself. What text does this Java code display?

int g = 2;
for (int t = 1; t <= 3; t++) {
    g += g;
}
System.out.print(g);
  1. A. 16
  2. B. 8
  3. C. 6
  4. D. 32

Question 9 of 10

A skipped header test is used on purpose. What text does this Java code display?

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

Question 10 of 10

How many times does this body run?

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