Output of Java Programs | Set 35 (Decision Making) - GeeksforGeeks

20 Sep, 2017
Prerequisite: Decision Making in JavaPredict the Output of the following programs
1. What will be the output of the following program?
publicclass Test {public    static void main(String[] args)    {        int x = 10;        if (x) {            System.out.println("HELLO GEEKS");        } else {            System.out.println("BYE");        }    }}
Options:1. HELLO GEEKS2. Compile time error3. Runtime Error4. BYEOutput:
The answer is option (2).
Explanation: The argument of if statement should be Boolean type. By mistake if we are trying to provide any other data type then we will get compile-time error saying incompatible types. Here the argument is of int type, therefore we will get compile time error saying error: incompatible types: int cannot be converted to Boolean
2. What will be the output of the following program?
publicclass Test {public    static void main(String[] args)    {        int x = 10;        if (x)            System.out.println("HELLO GEEKS");        System.out.println("WELCOME");          else        {            System.out.println("BYE");        }    }}
Options:1. HELLO GEEKSWELCOME2. HELLO GEEKS3. BYE4. Compile time errorOutput:
The answer is option (4)
Explanation: Curly braces are optional in if part. Without curly braces only one statement is allowed under if. If we will try to give more than one statement then we will get compile time error saying error: ‘else’ without ‘if’.
3. What will be the output of the following program?
publicclass Test {public    static void main(String[] args)    {        int x = 10, y = 20;        if (x < y)            int a = 10;        else {            System.out.println("BYE");        }    }}
Options:1. 102. BYE3. NO output4. Compile time errorOutput:
The answer is option (4).
Explanation: In the above program, there is a declarative statement in the if statement without curly braces that’s why we will get compile time error saying Error: variable declaration not allowed here.
4.What will be the output of the following program?
publicclass Test {public    static void main(String[] args)    {        int x = 10, y = 20;        if (x < y) {            if (x > y) {                System.out.println("HELLO GEEKS");            } else {                System.out.println("WELCOME");            }        }    }}
Output:1. HELLO GEEKS2. Compile time error3. WELCOME4. No Output
The answer is option (3)
Explanation: There is no dangling else problem in java. Every else is mapped to the nearest if statement.Here the inner else if mapped with the nearest if part i.e. if(x>y).
5. What will be the output of the following program?
publicclass Test {public    static void main(String[] args)    {        if (true)            ;    }}
Options:1. No Output2. Compile time error3. Runtime error4. Runtime ExceptionOutput:
 The answer is option (1)
Explanation: ;(semicolon) is a valid java statement which is also known as empty statement. Therefore we can apply it in if statement also.
6. What will be the output of the following program?
class Test {public    static void main(String[] args)    {        String day = "Sunday";        switch (day) {        case "Monday":            System.out.println("Let's Work");            break;        case "Saturday":            System.out.println("waiting for Sunday");            break;        case "Sunday":            System.out.println("Today is fun day");        }    }}
Options:1. Compile time error2. Lets work3. Run- time error4. Today is fun dayOutput:
The answer is option (4)
Explanation: The allowed arguments type for the switch statement are byte, short, char, int until 1.4 version. But from 1.5 version onward corresponding wrapper classes and enum type also allowed. From 1.7 version onward String type also allowed. Refer this article for detail
7. What will be the output of the following program?
publicclass MainClass {    enum day { MON,               SAT,               SUN } public static void main(String[] args)    {        day ch = day.SUN;        switch (ch) {        case MON:            System.out.println("Lets work!");            break;        case SAT:            System.out.println("Waiting for sunday");            break;        case SUN:            System.out.println("Lets have fun!");            break;        }    }}
Options:1. No Output2. Syntax error3. Lets have fun!4. Compile time errorOutput:
The answer is option (3)
Explanation: The allowed arguments type for the switch statement are byte, short, char, int until 1.4 version. But from 1.5 version onward corresponding wrapper classes and enum type also allowed. From 1.7 version onward String type also allowed.
8. What will be the output of the following program?
class MainClass {public    static void main(String[] args)    {        int x = 10;        Switch(x)        {            System.out.println("GEEKS");        }    }}
Options:1. GEEKS2. Compile time error3. No Output4. Run-time errorOutput:
The answer is option (2)
Explanation: Inside switch every statement should be under some case or default i.e. independent are not allowed inside switch otherwise we will get compile time error saying error:’;’ expected.
9. What will be the output of the following program?
class MainClass {public    static void main(String[] args)    {        int x = 10;        int y = 20;        switch (x) {        case 10:            System.out.println("HELLO");            break;        case y:            System.out.println("GEEKS");            break;        }    }}
Options:1. HELLO2. No Output3. GEEKS4. Compile time errorOutput:
The answer is option (4)
Explanation: Every case label should be constant otherwise we will get compile time error. But we can add variable as case label but we have to declare that variable as final. But here we are using variable y as a case label that’s why we will get compile time error saying error: constant expression required.
10. What will be the output of the following program?
class MainClass {public    static void main(String[] args)    {        int x = 10;        final int y = 20;        switch (x) {        case 10:            System.out.println("HELLO");            break;        case y:            System.out.println("GEEKS");            break;        }    }}
Options:1. GEEKS2. Compile time error3. HELLO4. NO OutputOutput:
The answer is option (3)
Explanation: Every case label should be constant otherwise we will get compile time error. But we can add variable as case label but we have to declare that variable as final. But here we are using variable y as a case label that’s why we will get result as HELLO.
11. What will be the output of the following program?
class MainClass {public    static void main(String[] args)    {        int x = 10;        switch (x + 1 + 1) {        case 10:            System.out.println("HELLO");            break;        case 10 + 1 + 1:            System.out.println("GEEKS");            break;        }    }}
Output:1. Compile time error2. GEEKS3. HELLO4. No Output
The answer is option (2).
Explanation: Both Switch argument and case label can be expressions. But case label should be constant expression.Here the case label “10+1+1” is treated as case 12 and switch argument “x+1+1” is also treated as 12.
12. What will be the output of the following program?
class MainClass {public    static void main(String arg[])    {        char stream = 'C';        int x = 2;          switch (x) {        case 1:            System.out.println("SCIENCE, MATHS, PHYSICS");            break;        case 2:            switch (stream) {            case 'A':                System.out.println("Welcome");                break;            case 'C':                System.out.println("Geeksforgeeks");                break;            case 'B':                System.out.println("Have a nice day");                break;            }            break;        case 3:            switch (stream) {            case 'C':                System.out.println("Welcome");                break;            case 'D':                System.out.println("In");                break;            case 'E':                System.out.println("GFG");                break;            }            break;        }    }}
Options:1. Compile time error2. GFG3. No Output4. GeeksforgeeksOutput:
The answer is option (4)
Explanation: Nested switch case is possible in java. A nested switch statement is a switch statement within another switch 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 Java program | Set 18 (Overriding)
Output of C++ programs | Set 34 (File Handling)
Different ways to copy a string in C/C++
Output of Python Program | Set 1
Output of C++ programs | Set 50
Runtime Errors
Output of Java program | Set 28
Output of C Programs | Set 2
C++ Programming Multiple Choice Questions
Output of Java Program | Set 20 (Inheritance)