while Loops MCQ Practice

Question 1 of 10

A prize booth starts with 18 tokens and must keep handing out 2 tokens as long as at least 2 remain. Which statement best explains why a loop is the right structure?

  1. A. A loop repeats the hand-out until fewer than 2 tokens remain.
  2. B. An if statement subtracts 2 over and over without being rewritten.
  3. C. Eighteen sequential print statements stop automatically when 0 is reached.
  4. D. A loop is allowed only when the starting count is unknown when the program is written.

Question 2 of 10

A cafe tracks unused cups. What happens when this Java code runs?

int cups = 4;
while (cups > 0) {
    System.out.print(cups);
}
  1. A. It displays 4321 and stops.
  2. B. It displays 4 and then stops.
  3. C. It displays 4 repeatedly without stopping.
  4. D. It displays nothing because the body is skipped.

Question 3 of 10

A sidewalk logger starts with no snow. What text does this Java code display?

int snow = 0;
while (snow > 0) {
    snow--;
    System.out.print("flake");
}
System.out.print("clear");
  1. A. flakeflakeflake repeating without stopping
  2. B. flake
  3. C. clear
  4. D. flakeclear

Question 4 of 10

A student wants the body to run once for each of the values 1 through 5. What value does this code store in count?

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

Question 5 of 10

A countdown display uses this loop. What text does this Java code display?

int n = 3;
while (n > 0) {
    System.out.print(n);
    n--;
}
  1. A. 321
  2. B. 210
  3. C. 32
  4. D. 3210

Question 6 of 10

A scoreboard is already at its goal. What text does this Java code display?

int goal = 10;
int score = 10;
int adds = 0;
while (score < goal) {
    score++;
    adds++;
}
System.out.print(adds);
  1. A. 0
  2. B. 1
  3. C. 10
  4. D. 9

Question 7 of 10

A clerk totals small invoices. What text does this Java code display?

int n = 1;
int sum = 0;
while (n < 5) {
    sum += n;
    n++;
}
System.out.print(sum + " " + n);
  1. A. 10 5
  2. B. 15 5
  3. C. 10 4
  4. D. 6 5

Question 8 of 10

A beacon flashes selected numbers. What text does this Java code display?

int k = 1;
while (k <= 9) {
    System.out.print(k);
    k += 4;
}
  1. A. 159
  2. B. 15913
  3. C. 15
  4. D. 123456789

Question 9 of 10

A sorter counts even remaining chips. What text does this Java code display?

int n = 7;
int even = 0;
while (n > 1) {
    if (n % 2 == 0) {
        even++;
    }
    n--;
}
System.out.print(even + " " + n);
  1. A. 3 1
  2. B. 4 1
  3. C. 3 0
  4. D. 2 1

Question 10 of 10

Two markers hop toward each other. What text does this Java code display?

int a = 2;
int b = 11;
int hops = 0;
while (a < b) {
    a += 3;
    b -= 1;
    hops++;
}
System.out.print(hops + " " + a + " " + b);
  1. A. 3 11 8
  2. B. 2 8 9
  3. C. 3 8 8
  4. D. 4 14 7