
Let’s test your understanding of the Java while loop MCQs! Solve the following question to practice how this loop ensures dynamic control over iterations, improving logic implementation in your code.
The Java while loop is a control structure that runs a block of code repeatedly as long as the condition is true. It’s particularly useful when the number of iterations isn’t fixed and depends on the program’s logic.
while (condition) {
// Code to execute
}
If you’re curious to explore more about the while
loop, check out the official Java documentation.
Java While Loop MCQs (Easy)
Now, let’s test your understanding of the while
loop! Solve the following MCQ to practice how this loop ensures dynamic control over iterations, improving logic implementation in your code.
What is the output of the following code?
int i = 0;
while (i++ < 5) {
System.out.print(i + " ");
}
View Answer
Correct Answer: D
The post-increment happens after the condition is checked but before the body of the loop executes. Therefore, the numbers 1 to 5 are printed.
What happens when the following code executes?
int x = 0;
while (x < 5)
x++;
System.out.println(x);
View Answer
Correct Answer: C
The variable `x` increments from 0 to 5, exiting the loop when `x` equals 5. The final value of `x`, which is 5, is printed.
Which of the following will cause a compilation error?
View Answer
Correct Answer: B
Java does not allow non-boolean expressions like `1` in a `while` condition. The other options are valid.
What is the issue with this code?
int i = 10;
while (i > 0)
System.out.println("i = " + i);
View Answer
Correct Answer: B
The value of `i` is never decremented, resulting in an infinite loop.
When does a `while` loop terminate?
View Answer
Correct Answer: A
A `while` loop terminates either when its condition evaluates to `false` or when a `break` statement is encountered.
What is the output of the following code?
int i = 1;
while (i < 10) {
System.out.println(i);
i *= 2;
}
View Answer
Correct Answer: C
The value of `i` doubles each iteration: 1, 2, 4, 8. When `i` becomes 16, it exits the loop.
Identify the error in this code.
int count = 0;
while (count = 5) {
System.out.println("Hello");
count++;
}
View Answer
Correct Answer: C
The assignment `count = 5` is not valid in the `while` condition, as it does not return a boolean.
What is the output?
int i = 0;
while (i < 3)
System.out.println(++i);
View Answer
Correct Answer: C
The pre-increment increases the value of `i` before it is printed, resulting in 1, 2, 3.
Which scenario causes a runtime exception?
View Answer
Correct Answer: C
The condition `1/0` causes a `ArithmeticException` because division by zero is not allowed.
What is the output of this code?
int i = 5;
while (--i > 0) {
System.out.print(i + " ");
}
View Answer
Correct Answer: B
The pre-decrement decreases the value of `i` before it is compared, so the output is 4, 3, 2, 1.
What is the output of this code?
int x = 0;
while (x++ < 3) {
if (x % 2 == 0) break;
System.out.print(x);
}
View Answer
Correct Answer: C
The loop breaks when `x` becomes 2, so only 1 is printed.
What is the output of the following code?
int i = 1;
while (i < 5) {
System.out.print(i + " ");
if (i == 3) break;
i++;
}
View Answer
Correct Answer: C
The loop prints 1 2 3 before breaking when `i` reaches 3.
What happens if the loop condition always evaluates to true?
View Answer
Correct Answer: C
A `while(true)` loop runs infinitely unless a condition like `break` or a modification to the loop variable stops it.
What is the output of the following code?
int i = 10;
while (i-- > 0) {
System.out.print(i + " ");
}
View Answer
Correct Answer: B
The `i–` causes the value of `i` to decrement, printing the numbers from 9 to 0.
What is the output of the following code?
int i = 0;
while (i < 5) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
i++;
}
View Answer
Correct Answer: C
The `continue` statement skips the even numbers (0, 2, 4) and prints only 1 and 3.
What is the output of the following code?
int i = 0;
while (i < 5) {
if (i == 3) break;
System.out.print(i + " ");
i++;
}
View Answer
Correct Answer: B
The loop breaks when `i` reaches 3, so the output is 0 1 2.
What is the output of the following code?
int i = 5;
while (i-- > 0) {
System.out.println(i);
}
View Answer
Correct Answer: D
The `i–` causes the loop to print values from 4 down to 0.
Which of the following will cause an infinite loop?
View Answer
Correct Answer: D
Both `while (true)` and `while (1 == 1)` are infinite loops because the condition is always true.
What is the output of the following code?
int i = 0;
while (i < 3) {
System.out.print(i + " ");
i = i + 2;
}
View Answer
Correct Answer: A
The value of `i` is incremented by 2 on each iteration, so the loop prints 0 and 2.
What is the output of the following code?
int i = 0;
while (i < 3) {
i++;
System.out.print(i + " ");
}
View Answer
Correct Answer: D
The value of `i` is incremented at the start of each iteration, so the output is 1, 2, 3.
Leave a Reply