Output of Java Programs | Set 37 (If-else)

25 Sep, 2017
Prerequisite: if else, for loops
1. What will be the output for the following program?
publicclass Test {public    static void main(String[] args)    {        for (;;)            System.out.println("GEEKS");    }}
Options:1.GEEKS2.Compile time error3.Run time Exception4.GEEKS (Infinitely)
The answer is option (4)
Explanation: In the above example, we are using for loop. In for loop if we did not provide any initialization, condition-check and increment/decrement part then it will go to infinite loop if we did not provide any condition in statement.
2. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        for (int i = 0; i < 3            System.out.println("GEEKS");    }}
Options:1.GEEKS GEEKS GEEKS2.Compile time error3.GEEKS (Infinitely)4.No output
 The answer is option (3)
Explanation: When we are not taking any statement in increment/decrement section therefore overtime it does not increment/decrement the value of I and the condition always true. That’s why it results into GEEKS (Infinitely).
3. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        boolean b = true;        if (b = false) {            System.out.println("HELLO");        } else {            System.out.println("BYE");        }    }}
Option:1.HELLO2.BYE3.Compile time error: re- initialization4.No output
The answer is option (2)
Explanation : In the condition of if statement, we assigning are false to b which return a boolean value which is false. Therefore the control goes to the else part and the output is BYE.
4. What will be the output for the following program?
publicclass Test {public    static void main(String[] args)    {        int a = 10, b = 20;        if (a < b) {            if (a > b) {                System.out.println("HELLO GEEKS");            } else {                System.out.println("WELCOME");            }        }    }}
Option:1.HELLO GEEKS2.WELCOME3.Compile time error4.HELLO GEEKS WELCOME
 The answer is option (2)
Explanation: Here we are defining nested if and a single else part. In java, there is no dangling else problem in java. Every else is mapped to the nearest if statement. Therefore the else part belongs to if(x>y) in the above program, which returns false that’s why control goes to else part and the output is WELCOME.
5. What will be the output for the following program?
class Test {public    static void main(String[] args)    {        for (int i = 0;; i++) {            System.out.println("HIII");        }        System.out.println("BYE");    }}
Options:1. HIII2. HIII(infinitely)3. BYE4. Compile time error
 The answer is option (4)
Explanation: In the above for loop it will go for infinite loop and the above program does not give any chance to the next lines of the program. That’s why compiler will give 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
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of C++ programs | Set 50
Runtime Errors
C++ Programming Multiple Choice Questions
Different ways to copy a string in C/C++
Output of C Programs | Set 2
Output of C++ Program | Set 1
Output of Python Program | Set 1
Output of C++ programs | Set 47 (Pointers)
Output of C programs | Set 59 (Loops and Control Statements)
Output of C Programs | Set 3