Java Topic Practice
While Loops
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
For practice use only.
While Loops MCQ Practice
While Loops Java practice with 15 questions, code tracing, and a worked explanation for every answer.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - While Loops: track the final printed value.
int value = 18;
int count = 0;
while (value > 1) {
value /= 2;
count++;
}
System.out.print(count + ":" + value);All 15 while loops questions
Work through the interactive quiz above first. This is the full while loops question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - While Loops: track the final printed value.
int value = 18; int count = 0; while (value > 1) { value /= 2; count++; } System.out.print(count + ":" + value);- 4:1
- 5:1
- 4:0
- 3:1
Show worked solution
Correct answer: 4:1
Integer division halves the value each pass and discards any remainder. The loop stops as soon as value is no longer greater than 1.
2.Code Tracing 12 - While Loops: follow the variable updates.
int x = 7; int total = 0; do { total += x; x -= 5; } while (x > 26); System.out.print(total + ":" + x);- 14:2
- 7:2
- 0:7
- 7:7
Show worked solution
Correct answer: 7:2
A do-while always runs its body once before testing the condition, so the body executes even though the condition is false on the first check.
3.Code Tracing 28 - While Loops: trace the branch and loop path.
int a = 38; int b = 3; int steps = 0; while (a > 0 && b < 10) { a -= 3; b += 1; steps++; } System.out.print(steps + ":" + a + ":" + b);- 0:38:3
- 7:17:10
- 8:14:11
- 7:17:3
Show worked solution
Correct answer: 7:17:10
The loop continues only while both conditions hold. It stops as soon as either one fails.
4.Code Tracing 39 - While Loops: watch the index changes.
String out = ""; int i = 2; while (out.length() < 6) { out += i; i++; } System.out.print(out + ":" + i);- 23456:8
- 2345678:9
- 234567:8
- 234567:7
Show worked solution
Correct answer: 234567:8
The condition tests the string length, not the counter, so the loop stops as soon as the built string is long enough.
5.Code Tracing 50 - While Loops: evaluate the state change step by step.
int sum = 0; int k = 1; while (true) { sum += k; if (sum > 54) break; k++; } System.out.print(k + ":" + sum);- 11:55
- 10:45
- 9:55
- 10:55
Show worked solution
Correct answer: 10:55
The break exits before k is incremented again, so k keeps the value it had when the sum first passed the limit.
6.What is printed by the following code segment?
int n = 1; int c = 0; while (n < 200) { n = n * 3 + 1; c++; } System.out.print(n + " " + c);- 121 4
- 364 4
- 121 5
- 364 5
Show worked solution
Correct answer: 364 5
The values run 4, 13, 40, 121, then 364, which is the first to reach the bound.
7.What is printed by the following code segment?
int a = 20; int b = 8; while (a != b) { if (a > b) { a -= b; } else { b -= a; } } System.out.print(a);- 4
- 8
- 2
- 20
Show worked solution
Correct answer: 4
Repeated subtraction finds the greatest common divisor, which for 20 and 8 is 4.
8.What is printed by the following code segment?
int n = 37; int d = 2; while (n % d != 0) { d++; } System.out.print(d);- 2
- 37
- 3
- 1
Show worked solution
Correct answer: 37
No divisor below 37 divides it evenly, so the loop runs until d reaches the number itself.
9.What is printed by the following code segment?
int x = 100; int c = 0; while (x > 1) { x = x % 2 == 0 ? x / 2 : x * 3 + 1; c++; } System.out.print(c);- 12
- 18
- 25
- 10
Show worked solution
Correct answer: 25
This is the Collatz sequence starting at 100, which reaches 1 after 25 steps.
10.What is printed by the following code segment?
int s = 0; int i = 1; while (i * i <= 50) { s += i * i; i++; } System.out.print(s);- 204
- 91
- 55
- 140
Show worked solution
Correct answer: 140
The squares up to 49 are 1, 4, 9, 16, 25, 36, and 49.
11.What is printed by the following code segment?
String s = "abcdef"; int i = 0; String out = ""; while (i < s.length()) { out += s.charAt(i); i += 2; } System.out.print(out);- ace
- abc
- bdf
- abcdef
Show worked solution
Correct answer: ace
Stepping by two visits indexes 0, 2, and 4.
12.What is printed by the following code segment?
int n = 5; int f = 1; while (n > 0) { f *= n; n -= 2; } System.out.print(f);- 120
- 15
- 10
- 5
Show worked solution
Correct answer: 15
Only the odd values 5, 3, and 1 are multiplied together.
13.What is printed by the following code segment?
int a = 0; int b = 1; while (b < 30) { int t = a + b; a = b; b = t; } System.out.print(a + " " + b);- 13 21
- 34 55
- 21 34
- 21 21
Show worked solution
Correct answer: 21 34
The Fibonacci pair advances until the second value first reaches 30 or more, which is 34.
14.What is printed by the following code segment?
int n = 9; int sum = 0; while (n != 0) { sum += n % 10; n /= 10; if (n == 0 && sum > 9) { n = sum; sum = 0; } } System.out.print(sum);- 0
- 18
- 1
- 9
Show worked solution
Correct answer: 9
A single digit sums to itself, and the reset never triggers because the sum stays below 10.
15.What is printed by the following code segment?
int c = 0; int n = 1000; while (n >= 1) { n /= 4; c++; } System.out.print(c);- 5
- 6
- 7
- 4
Show worked solution
Correct answer: 5
Dividing by 4 gives 250, 62, 15, 3, and then 0, so the body runs five times.
