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

Prerequisite : while loop in Java

1. What will be the output for the following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
public
    static void main(String[] args)
    {
        do
            while (true)
                System.out.println("HELLO");
        while (false);
    }
}

chevron_right


Options:
1. HELLO
2. Compile time error
3. 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
public
    static void main(String[] args)
    {
        do
            System.out.println("FRIENDS");
        while (true);
        System.out.println("ENEMY");
    }
}

chevron_right


Options:
1. Compile time error
2. FRIENDS
3. No output
4. 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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");
    }
}

chevron_right


Options:
1. FRIENDS
2. ENEMY
3. No Output
4. FRIENDS (Infinitely)

 The answer is option (4)

Explaination: 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
public
    static void main(String[] args)
    {
        do
            while (true)
                ;
        System.out.println("HELLO");
    }
}

chevron_right


Options:
1 HELLO
2 HELLO (Infinitely)
3 Error: Unreachable statement
4 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?

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test {
public static void main(String[] args)
    {
        do {
            System.out.print(1);
            do {
  
                System.out.print(2);
  
            } while (false);
  
        } while (false);
    }
}

chevron_right


Options:
1. 12
2. 21
3. 1
4. 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.



My Personal Notes arrow_drop_up


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.