Java While Do while loop quiz contains 20 single and multiple choice questions. While Do While loop quiz questions are designed in such a way that it will help you understand how while and do while loop works in Java. At the end of the quiz, result will be displayed along with your score and Java while do while loop quiz answers.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java While Do While quiz online.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 20 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
While Do While Loop0%
Review Answers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Answered
Review
Question 1 of 20
1. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0;
while(a < 10){
System.out.println(a++);
}
}
}
Correct answer.
Option 3 is the correct choice. Variable a is started with zero. Since a is incremented using post increment operator (a++), its value is printed first and then value gets incremented. When a is 9 then it is printed and incremented to 10, so in next while iteration a < 10 becomes false. Program will print 0 to 9.
Incorrect answer.
Option 3 is the correct choice. Variable a is started with zero. Since a is incremented using post increment operator (a++), its value is printed first and then value gets incremented. When a is 9 then it is printed and incremented to 10, so in next while iteration a < 10 becomes false. Program will print 0 to 9.
Question 2 of 20
2. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0, b = 5;
while(b--){
System.out.println(a++);
}
}
}
Correct answer.
Option 1 is the correct choice. While loop condition must be of type boolean.
Incorrect answer.
Option 1 is the correct choice. While loop condition must be of type boolean.
Question 3 of 20
3. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0;
while(a <= 10){
a++;
}
System.out.println(a);
}
}
Correct answer.
Option 2 is the correct choice. When a becomes 10, while loop condition (a <= 10) evaluates to true and control goes inside the loop where it is incremented to 11. However, in the next iteration while loop condition becomes false and value of a is printed as 11.
Incorrect answer.
Option 2 is the correct choice. When a becomes 10, while loop condition (a <= 10) evaluates to true and control goes inside the loop where it is incremented to 11. However, in the next iteration while loop condition becomes false and value of a is printed as 11.
Question 4 of 20
4. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0;
while(a < 10){
System.out.println(++a);
}
}
}
Correct answer.
Option 4 is the correct choice. Value of a is incremented using pre increment operator so value is incremented first and printed later. Hence output starts with 1 in the first iteration of the loop. When a becomes 9, while loop condition is still true and a is incremented to 10 and printed. Condition become false thereafter since a is no more less than 10. Program will print 1 to 10.
Incorrect answer.
Option 4 is the correct choice. Value of a is incremented using pre increment operator so value is incremented first and printed later. Hence output starts with 1 in the first iteration of the loop. When a becomes 9, while loop condition is still true and a is incremented to 10 and printed. Condition become false thereafter since a is no more less than 10. Program will print 1 to 10.
Question 5 of 20
5. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0, b = 10;
while( a + 2 < b){
System.out.println(a);
}
}
}
Correct answer.
Option 4 is the correct choice. It is very easy to overlook that value of a is never incremented inside the loop. It stays 0 forever and the while loop condition a + 2 < b always evaluates to true. The loop never ends and prints 0 infinitely. This kind of loop is called infinite loop.
Incorrect answer.
Option 4 is the correct choice. It is very easy to overlook that value of a is never incremented inside the loop. It stays 0 forever and the while loop condition a + 2 < b always evaluates to true. The loop never ends and prints 0 infinitely. This kind of loop is called infinite loop.
Question 6 of 20
6. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0, b = 5;
while( b-- > a++)
System.out.println(a);
}
}
Correct answer.
Option 4 is the correct choice. In while loop condition, b is decremented and a is incremented simultaneously using post decrement and post increment operators respectively. Hence values are compared first and decremented and incremented later. Output starts with 1. When a is 2 and b is 3 in third iteration, the while condition is still true, however, they are immediately incremented and decremented to 3 and 2 respectively and printed. In the next iteration, the condition becomes false so program will print 1 to 3.
Incorrect answer.
Option 4 is the correct choice. In while loop condition, b is decremented and a is incremented simultaneously using post decrement and post increment operators respectively. Hence values are compared first and decremented and incremented later. Output starts with 1. When a is 2 and b is 3 in third iteration, the while condition is still true, however, they are immediately incremented and decremented to 3 and 2 respectively and printed. In the next iteration, the condition becomes false so program will print 1 to 3.
Question 7 of 20
7. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0, b = 10;
while( --b > a )
System.out.println(a);
a++;
}
}
Correct answer.
Option 4 is the correct choice. Since while loop does not have curly braces only System.out.println statement is considered part of it and a++ statement outside of the loop. Since a is never incremented inside the loop, it stays at zero till the value of b becomes 0 which makes while condition false. Code will print zero 9 times.
Incorrect answer.
Option 4 is the correct choice. Since while loop does not have curly braces only System.out.println statement is considered part of it and a++ statement outside of the loop. Since a is never incremented inside the loop, it stays at zero till the value of b becomes 0 which makes while condition false. Code will print zero 9 times.
Question 8 of 20
8. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 100;
while(int b = 10){
a -= 10;
b += 10;
System.out.println(a);
}
}
}
Correct answer.
Option 1 is the correct choice. While loop condition must be of type boolean, variable declaration is not allowed there. Hence program will give compilation error.
Incorrect answer.
Option 1 is the correct choice. While loop condition must be of type boolean, variable declaration is not allowed there. Hence program will give compilation error.
Question 9 of 20
9. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0;
while(a + 2 < 10){
a += 2;
int b = 10;
while(b - 2 > 0){
b -= 2;
}
}
System.out.println(a + " " + b);
}
}
Correct answer.
Option 4 is the correct choice. Since variable b is declared inside inner while loop, it is local to that block only and cannot be accessed outside that loop. Hence, program will give compilation error “b cannot be resolved to a variable”.
Incorrect answer.
Option 4 is the correct choice. Since variable b is declared inside inner while loop, it is local to that block only and cannot be accessed outside that loop. Hence, program will give compilation error “b cannot be resolved to a variable”.
Question 10 of 20
10. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 10, b = 0;
while( a + 2 < 10 && b - 2 > 0){
System.out.println(a);
}
}
}
Correct answer.
Option 4 is the correct choice. The while condition is always false since values of a and b variables are never incremented or decremented. Program will not print anything when run.
Incorrect answer.
Option 4 is the correct choice. The while condition is always false since values of a and b variables are never incremented or decremented. Program will not print anything when run.
Question 11 of 20
11. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 0, b = 10;
while(b - 2 > 0){
b -= 2;
while(a + 2 < 10){
a += 2;
if(a == b)
continue;
System.out.print(a + " " + b + ", ");
}
}
}
}
Correct answer.
Option 2 is the correct choice. Variable a is declared outside both the loops, so after first iteration of the outer while loop, value of variable a becomes 8 and stays at that value. Hence, nothing will be printed from second iteration onward due to while condition of the inner loop being always false ( 8+2 < 10 ).
Incorrect answer.
Option 2 is the correct choice. Variable a is declared outside both the loops, so after first iteration of the outer while loop, value of variable a becomes 8 and stays at that value. Hence, nothing will be printed from second iteration onward due to while condition of the inner loop being always false ( 8+2 < 10 ).
Question 12 of 20
12. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 3, b = 0;
while( a >= 0 ){
a -= 1;
while(b<=3 ){
b += 1;
if(a < b)
break;
System.out.print(a + " " + b + ", ");
}
}
}
}
Correct answer.
Option 3 is the correct choice. Variable b is declared before both the loops, it is not reset to zero before inner while loop. In the first iteration of outer while loop, a is 3 – 1 i.e. 2 and b starts from 0 + 1 i.e. 1. The values are printed if and only if a >= b (because of break statement if a < b condition). So program will print 2 1 and 2 2. However, once b becomes 3, a < b condition becomes true and will not print anything then onwards.
Incorrect answer.
Option 3 is the correct choice. Variable b is declared before both the loops, it is not reset to zero before inner while loop. In the first iteration of outer while loop, a is 3 – 1 i.e. 2 and b starts from 0 + 1 i.e. 1. The values are printed if and only if a >= b (because of break statement if a < b condition). So program will print 2 1 and 2 2. However, once b becomes 3, a < b condition becomes true and will not print anything then onwards.
Question 13 of 20
13. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 0;
outer: while(i < 10){
i+=2;
int j = 10;
inner: while(j > 0){
j -=2;
if(i == j)
break outer;
System.out.print(i + " " + j + ", ");
}
}
}
}
Correct answer.
Option 2 is the correct choice. Variable a goes from 0 to 10 in increments of 2, variable b goes 10 to 0 in decrements of 2. if a == b, control goes to the break statement. Both the loops are marked with the labels. However, code breaks the outer loop, so whenever code encounters a == b first time, control goes out of the loop and rest of the iterations are never executed. In first iteration of inner loop 2 2 will be printed. in second iteration 2 6 and in third iteration of inner loop 2 6 will be printed. However, in forth iteration of the inner loop, both a and b are equal to 2 so control goes to the break statement which breaks both the loops not just the inner loop.
Incorrect answer.
Option 2 is the correct choice. Variable a goes from 0 to 10 in increments of 2, variable b goes 10 to 0 in decrements of 2. if a == b, control goes to the break statement. Both the loops are marked with the labels. However, code breaks the outer loop, so whenever code encounters a == b first time, control goes out of the loop and rest of the iterations are never executed. In first iteration of inner loop 2 2 will be printed. in second iteration 2 6 and in third iteration of inner loop 2 6 will be printed. However, in forth iteration of the inner loop, both a and b are equal to 2 so control goes to the break statement which breaks both the loops not just the inner loop.
Question 14 of 20
14. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 4, b = 4;
while(a-- > 0){
while(b-- > 0){
if(a == b)
continue;
System.out.print(a + " " + b + ", ");
}
}
}
}
Correct answer.
Option 1 is the correct choice. Continue statement skips the current iteration of the inner most loop and goes to the next iteration. Plus, a and b are declared outside both the loops. So once variable b becomes 1 in the first iteration of the outer loop and third iteration of inner loop, the inner while condition will always be false then onwards.
Incorrect answer.
Option 1 is the correct choice. Continue statement skips the current iteration of the inner most loop and goes to the next iteration. Plus, a and b are declared outside both the loops. So once variable b becomes 1 in the first iteration of the outer loop and third iteration of inner loop, the inner while condition will always be false then onwards.
Question 15 of 20
15. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int a = 4, b = 4;
outer: while(a-- > 0){
while(b-- > 0){
if(a == b)
continue outer;
System.out.print(a + " " + b + ", ");
}
}
}
}
Correct answer.
Option 4 is the correct choice. The code will not print anything! That’s because a and b both are initialized to 4 and decremented at the same time. So in every first iterations of the inner loop, if condition becomes true and code will continue with the next iteration of the outer loop.
Incorrect answer.
Option 4 is the correct choice. The code will not print anything! That’s because a and b both are initialized to 4 and decremented at the same time. So in every first iterations of the inner loop, if condition becomes true and code will continue with the next iteration of the outer loop.
Question 16 of 20
16. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
while(1){
System.out.println("Hello");
}
}
}
Correct answer.
Option 1 is the correct choice. Program will give compilation error. Condition of the while loop must be of type boolean unlike C/C++ where you could mention 0 or 1.
Incorrect answer.
Option 1 is the correct choice. Program will give compilation error. Condition of the while loop must be of type boolean unlike C/C++ where you could mention 0 or 1.
Question 17 of 20
17. Question
Requirement is to read the user input line from the console using loop and write the input to a file line by line. If the user enters “END”, it should be written to the file and the program should exit. Which loop would you use?
Correct answer.
Option 2 is the correct choice. Since the requirement is to read at least one input line from console and write to a file before exiting, do while loop is a better choice. Do while loop ensures that there is always one iteration of the loop before exiting (because do while loop executes code block first and checks the condition later).
Incorrect answer.
Option 2 is the correct choice. Since the requirement is to read at least one input line from console and write to a file before exiting, do while loop is a better choice. Do while loop ensures that there is always one iteration of the loop before exiting (because do while loop executes code block first and checks the condition later).
Question 18 of 20
18. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 0;
do{
System.out.println("Loop");
}while(i != 0);
}
}
Correct answer.
Option 2 is the correct choice. Do while loop always has one iteration of the code block regardless of the condition. Code will execute and print “Loop”, then the condition will be checked which is false and hence the loop will be terminated.
Incorrect answer.
Option 2 is the correct choice. Do while loop always has one iteration of the code block regardless of the condition. Code will execute and print “Loop”, then the condition will be checked which is false and hence the loop will be terminated.
Question 19 of 20
19. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
int i = 0;
do{
i++;
System.out.println(i);
}while(i < 5)
}
}
Correct answer.
Option 1 is the correct choice. There must be a semi-colon after the while condition which is missing. Correct statement would be “while(i < 5);".
Incorrect answer.
Option 1 is the correct choice. There must be a semi-colon after the while condition which is missing. Correct statement would be “while(i < 5);".
Question 20 of 20
20. Question
Regardless of the boolean condition of the do while loop, there is always at least one iteration of the loop.
Correct answer.
True is the correct choice. Do while loop executes the code block once and then checks for the associated condition. If it is true, it continues to the next iteration otherwise control goes out of the loop to the next statement in code.
Incorrect answer.
True is the correct choice. Do while loop executes the code block once and then checks for the associated condition. If it is true, it continues to the next iteration otherwise control goes out of the loop to the next statement in code.