Open In App
Related Articles

Output of Java Programs | Set 43 (Conditional statements & Loops)

Improve Article
Improve
Save Article
Save
Like Article
Like

Prerequisite: Decision Control and Loops

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




class Test {
public
    static void main(String[] args)
    {
        int i = 0, j = 9;
        do {
            i++;
            if (j-- < i++) {
                break;
            }
        } while (i < 5);
        System.out.println(i + "" + j);
    }
}


Options:
1.44
2.55
3.66
4.77

The answer is option (3)

Explanation : In the above program, we have to specially take care about the break statement. The execution of the program is going as usual as the control flow of do-while loop but whenever compiler encountered break statement its control comes out from the loop.

2. What will be the output of the following program?




class Test {
public
    static void main(String[] args)
    {
        int j = 0;
        do
            for (int i = 0; i++ < 1😉
                System.out.println(i);
        while (j++ < 2);
    }
}


Options:
1. 111
2. 222
3. 333
4. error

The answer is option (1)

Explanation : As we all know that curly braces are optional in do and for loop. But the only criteria is if we declare a statement without curly statement, then the statement should not be declarative.

3. What will be the output of the following program?




class Test {
    static String s = "";
public
    static void main(String[] args)
    {
    P:
        for (int i = 2; i < 7; i++) {
            if (i == 3)
                continue;
            if (i == 5)
                break P;
            s = s + i;
        }
        System.out.println(s);
    }
}


Options:
1. 32
2. 23
3. 24
4. 42

 The answer is option (3)

Explanation : In the above example, when the first for loop is executed then it holds the value of i as 2. As long as the i value is 2, the loop will not execute the if condition and will be directly as s=s+i. Here s stores the value in a string format. Next time when s=s+i is executed, the i value becomes 4. Both these values are stored in the s in the form of a string.

4. What will be the output of the following program?




class Test {
public
    static void main(String[] args)
    {
        int x = 10;
        if (++x < 10 && (x / 0 > 10)) {
            System.out.println("Bishal");
        } else {
            System.out.println("GEEKS");
        }
    }
}


Options:
1.Compile time error
2. RuntimeException:ArithmeticException: / by zero
3. Bishal
4. GEEKS

The answer is option (4)

Explanation : In the above program we are using && operator (short-circuit operator). Whenever we use && operator then if the first condition is false then the control does not go to the 2nd condition whether it is true or false. In the above program, the first condition in the if block is not true that’s why the else part is executed.

5. What will be the output of the following program?




class Test {
public
    static void main(String[] args)
    {
        final int a = 10, b = 20;
        while (a > b) {
            System.out.println("Hello");
        }
        System.out.println("GEEKS");
    }
}


Options:
1. Compile time error
2. GEEKS
3. Hello
4. No Output

The answer is option (1)

Explanation: In the above program, we declare two variables as final. In the while loop, it always returns false and the control does not go inside while loop and it does not get the chance in the entire program. Thats why we will get compile time error saying error: unreachable statement.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 29 Sep, 2017
Like Article
Save Article
Similar Reads