Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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?




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. FRIENDS
2. ENEMY
3. No Output
4. 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 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?




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


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.



Last Updated : 18 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads