0
0
Javaprogramming~20 mins

Counter-based while loop in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple counter-based while loop
What is the output of the following Java code?
Java
int count = 1;
while (count <= 3) {
    System.out.print(count + " ");
    count++;
}
A1 2 3
B0 1 2 3
C1 2 3 4
D1 2
Attempts:
2 left
💡 Hint
Think about how the counter changes and when the loop stops.
Predict Output
intermediate
2:00remaining
Counting down with a while loop
What will this Java code print?
Java
int count = 5;
while (count > 2) {
    System.out.print(count + " ");
    count--;
}
A5 4 3 2
B5 4 3
C5 4 3 2 1
D4 3 2
Attempts:
2 left
💡 Hint
Look at the condition and how the counter decreases.
Predict Output
advanced
2:00remaining
Sum of numbers using a while loop
What is the value of sum after this code runs?
Java
int count = 1;
int sum = 0;
while (count <= 4) {
    sum += count;
    count++;
}
System.out.println(sum);
A10
B9
C11
D0
Attempts:
2 left
💡 Hint
Add numbers 1 through 4 together.
Predict Output
advanced
2:00remaining
Loop with incorrect counter update
What happens when this code runs?
Java
int count = 1;
while (count <= 3) {
    System.out.print(count + " ");
    // Missing count++ here
}
APrints nothing
BPrints 1 2 3 and stops
CSyntax error due to missing increment
DInfinite loop printing 1 repeatedly
Attempts:
2 left
💡 Hint
What happens if the counter never changes inside the loop?
🧠 Conceptual
expert
2:00remaining
Number of iterations in a counter-based while loop
How many times will the loop body execute in this code?
Java
int count = 2;
while (count < 10) {
    count += 3;
}
A4
B6
C3
D5
Attempts:
2 left
💡 Hint
Trace the values: start with count=2, add 3 each time until the condition fails.