Output of Java Programs | Set 36 (do-while loop) - GeeksforGeeks

18 Jun, 2020
Prerequisite : while loop in Java
1. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        do            while (true)                System.out.println("HELLO");        while (false);    }}
Options:1. HELLO2. Compile time error3. HELLO (infinitely)4. No Output
The answer is option (3)
Explanation: Think the above example like this
do 
       while(true)
           System.out.println("HELLO");
while(false);
This is the simple do-while loop. As we know that in while loop without curly braces we can take only one statement which should not be declarative. Here, HELLO is that one statement of while loop which is without curly braces. Here the inner while loop always gives true and the program output will HELLO infinitely.
2. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        do            System.out.println("FRIENDS");        while (true);        System.out.println("ENEMY");    }}
Options:1. Compile time error2. FRIENDS3. No output4. ENEMY
The answer is option (1)
Explanation: Here while loop always give true and it will print FRIENDS infinitely and did not give change to print ENEMY. Which gives compile time error saying
prog.java:8: error: unreachable statement
        System.out.println("ENEMY");
3. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        int x = 1, y = 2;        do            System.out.println("FRIENDS");        while (x < y);        System.out.println("ENEMY");    }}
Options:1. FRIENDS2. ENEMY3. No Output4. FRIENDS (Infinitely)
 The answer is option (4)
Explanation: Here at the compile time compiler thinks that x and y both are variables and its value can be changed and that will gives false to the while loop. Which gives the chance to print ENEMY that’s why compiler did not throw any compile time error.
4. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        do            while (true)                ;        System.out.println("HELLO");    }}
Options:1 HELLO2 HELLO (Infinitely)3 Error: Unreachable statement4 Error: ; expected
 The answer is option (4)
Explanation: In the above example there is a syntax error of do-while loop. As we all know that without curly braces we can take a statement which cannot be declarative but if we are not specifying any statement and not mentioning any curly braces then compiler will give an error saying Error: ; expected.
5. What will be the output for the following program?
class Test {public static void main(String[] args)    {        do {            System.out.print(1);            do {                  System.out.print(2);              } while (false);          } while (false);    }}
Options:1. 122. 213. 14. 2
The answer is option (1)
Explanation: In the above example, nesting in a do while will work under nesting process. First inner do-while loop will be executed after that the outer one.
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to copy a string in C/C++
Output of C++ programs | Set 34 (File Handling)
Output of C++ programs | Set 50
Output of Java program | Set 28
Output of Python Program | Set 1
Runtime Errors
Output of Java Program | Set 20 (Inheritance)
Output of C Programs | Set 2
How to show full column content in a PySpark Dataframe ?
C++ Programming Multiple Choice Questions