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

29 Sep, 2017
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.442.553.664.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. 1112. 2223. 3334. 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. 322. 233. 244. 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 error2. RuntimeException:ArithmeticException: / by zero3. Bishal4. 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 error2. GEEKS3. Hello4. 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.
Java-Output
Loops & Control Structure
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.