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

Prerequisite: Decision Control and Loops

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

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