• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

Java Tutorials Online

  • CORE JAVA
  • SERVLET

Java While Loop: Practice MCQs

Core Java While Loop MCQs

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.

Question 1:

What is the output of the following code?

int i = 0;
while (i++ < 5) {
    System.out.print(i + " ");
}
A
B
C
D
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.

Question 2:

What happens when the following code executes?

int x = 0;
while (x < 5)
    x++;
System.out.println(x);
A
B
C
D
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.

Question 3:

Which of the following will cause a compilation error?

A
B
C
D
View Answer

Correct Answer: B


Java does not allow non-boolean expressions like `1` in a `while` condition. The other options are valid.

Question 4:

What is the issue with this code?

int i = 10;
while (i > 0)
    System.out.println("i = " + i);
A
B
C
D
View Answer

Correct Answer: B


The value of `i` is never decremented, resulting in an infinite loop.

Question 5:

When does a `while` loop terminate?

A
B
C
D
View Answer

Correct Answer: A


A `while` loop terminates either when its condition evaluates to `false` or when a `break` statement is encountered.

Question 6:

What is the output of the following code?

int i = 1;
while (i < 10) {
    System.out.println(i);
    i *= 2;
}
A
B
C
D
View Answer

Correct Answer: C


The value of `i` doubles each iteration: 1, 2, 4, 8. When `i` becomes 16, it exits the loop.

Question 7:

Identify the error in this code.

int count = 0;
while (count = 5) {
    System.out.println("Hello");
    count++;
}
A
B
C
D
View Answer

Correct Answer: C


The assignment `count = 5` is not valid in the `while` condition, as it does not return a boolean.

Question 8:

What is the output?

int i = 0;
while (i < 3)
    System.out.println(++i);
A
B
C
D
View Answer

Correct Answer: C


The pre-increment increases the value of `i` before it is printed, resulting in 1, 2, 3.

Question 9:

Which scenario causes a runtime exception?

A
B
C
D
View Answer

Correct Answer: C


The condition `1/0` causes a `ArithmeticException` because division by zero is not allowed.

Question 10:

What is the output of this code?

int i = 5;
while (--i > 0) {
    System.out.print(i + " ");
}
A
B
C
D
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.

Question 11:

What is the output of this code?

int x = 0;
while (x++ < 3) {
    if (x % 2 == 0) break;
    System.out.print(x);
}
A
B
C
D
View Answer

Correct Answer: C


The loop breaks when `x` becomes 2, so only 1 is printed.

Question 12:

What is the output of the following code?

int i = 1;
while (i < 5) {
    System.out.print(i + " ");
    if (i == 3) break;
    i++;
}
A
B
C
D
View Answer

Correct Answer: C


The loop prints 1 2 3 before breaking when `i` reaches 3.

Question 13:

What happens if the loop condition always evaluates to true?

A
B
C
D
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.

Question 14:

What is the output of the following code?

int i = 10;
while (i-- > 0) {
    System.out.print(i + " ");
}
A
B
C
D
View Answer

Correct Answer: B


The `i–` causes the value of `i` to decrement, printing the numbers from 9 to 0.

Question 15:

What is the output of the following code?

int i = 0;
while (i < 5) {
    if (i % 2 == 0) continue;
    System.out.print(i + " ");
    i++;
}
A
B
C
D
View Answer

Correct Answer: C


The `continue` statement skips the even numbers (0, 2, 4) and prints only 1 and 3.

Question 16:

What is the output of the following code?

int i = 0;
while (i < 5) {
    if (i == 3) break;
    System.out.print(i + " ");
    i++;
}
A
B
C
D
View Answer

Correct Answer: B


The loop breaks when `i` reaches 3, so the output is 0 1 2.

Question 17:

What is the output of the following code?

int i = 5;
while (i-- > 0) {
    System.out.println(i);
}
A
B
C
D
View Answer

Correct Answer: D


The `i–` causes the loop to print values from 4 down to 0.

Question 18:

Which of the following will cause an infinite loop?

A
B
C
D
View Answer

Correct Answer: D


Both `while (true)` and `while (1 == 1)` are infinite loops because the condition is always true.

Question 19:

What is the output of the following code?

int i = 0;
while (i < 3) {
    System.out.print(i + " ");
    i = i + 2;
}
A
B
C
D
View Answer

Correct Answer: A


The value of `i` is incremented by 2 on each iteration, so the loop prints 0 and 2.

Question 20:

What is the output of the following code?

int i = 0;
while (i < 3) {
    i++;
    System.out.print(i + " ");
}
A
B
C
D
View Answer

Correct Answer: D


The value of `i` is incremented at the start of each iteration, so the output is 1, 2, 3.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Java Do-While Loop: Practice MCQs
  • Java Array MCQs: Easy-Level Practice Questions
  • Java While Loop: Practice MCQs
  • Java For-Each: Practice MCQs
  • Servlet Configuration MCQs: Master Java Servlets

Recent Comments

    Archives

    • January 2025

    Categories

    Copyright © 2025 · Magazine Pro on Genesis Framework · WordPress · Log in