while Loops MCQ Practice

Question 1 of 10

A kiln cools 5 degrees at a time until the temperature is no longer above 200. Which statement best explains why a while loop is appropriate?

  1. A. The cooling step should repeat while the temperature stays above 200.
  2. B. An if statement repeats automatically until the temperature drops.
  3. C. Java repeats every assignment until a variable reaches 200.
  4. D. A loop is illegal unless the exact number of cooling steps is written in a header.

Question 2 of 10

A tank logs shrinking moss. What happens when this Java code runs?

int moss = 2;
while (moss < 10) {
    moss -= 1;
    System.out.print(moss);
}
  1. A. It displays 1 and then stops.
  2. B. It displays 210 and then stops.
  3. C. It keeps displaying decreasing values without stopping.
  4. D. It displays nothing because the first check is false.

Question 3 of 10

A tide marker starts below the alert line. What text does this Java code display?

int tide = 4;
while (tide > 8) {
    tide--;
    System.out.print("in");
}
System.out.print("out");
  1. A. inout
  2. B. in
  3. C. out
  4. D. in repeating without stopping

Question 4 of 10

A ranger wants one mark for each post numbered 0 through 4 inclusive. What text does this Java code display?

int post = 0;
int marks = 0;
while (post < 4) {
    marks++;
    post++;
}
System.out.print(marks);
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 0

Question 5 of 10

A packing line peels leftover bags. What text does this Java code display?

int bag = 5;
while (bag > 2) {
    System.out.print(bag);
    bag--;
}
  1. A. 543
  2. B. 5432
  3. C. 54
  4. D. 532

Question 6 of 10

A greenhouse already sits at the heat limit. What text does this Java code display?

int heat = 9;
int fans = 0;
while (heat > 9) {
    heat--;
    fans++;
}
System.out.print(fans);
  1. A. 0
  2. B. 1
  3. C. 9
  4. D. 8

Question 7 of 10

Even lane numbers are totaled. What text does this Java code display?

int n = 2;
int sum = 0;
while (n <= 8) {
    sum += n;
    n += 2;
}
System.out.print(sum + " " + n);
  1. A. 20 10
  2. B. 20 8
  3. C. 30 10
  4. D. 14 10

Question 8 of 10

A sample is halved until it is no longer greater than 3. What text does this Java code display?

int p = 20;
while (p > 3) {
    System.out.print(p);
    p /= 2;
}
  1. A. 20105
  2. B. 201052
  3. C. 2010
  4. D. 1052

Question 9 of 10

A paper strip is folded while twice its length is still strictly less than 16. What text does this Java code display?

int left = 1;
int right = 16;
int folds = 0;
while (left * 2 < right) {
    left *= 2;
    folds++;
}
System.out.print(folds + " " + left);
  1. A. 3 8
  2. B. 4 16
  3. C. 3 16
  4. D. 2 4

Question 10 of 10

Two trail counters move together. What text does this Java code display?

int x = 1;
int y = 4;
int steps = 0;
while (x <= 10 && y > 0) {
    x += y;
    y--;
    steps++;
}
System.out.print(steps + " " + x + " " + y);
  1. A. 4 11 0
  2. B. 3 10 1
  3. C. 4 10 0
  4. D. 5 11 -1