Java While Do While Loop Quiz Online Test

Time limit: 0

Quiz-summary

0 of 20 questions completed

Questions:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Information

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.

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.

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.

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.

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.

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.

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.

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”.

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 ).

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.

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.

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.

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.

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?

while loop

do while loop

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).

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.

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.

True

False

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.