Open In App

Output of Java Programs | Set 35 (Decision Making)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Decision Making in Java
Predict the Output of the following programs

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




public
class 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 GEEKS
2. Compile time error
3. Runtime Error
4. BYE
Output:

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?




public
class 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 GEEKS
WELCOME
2. HELLO GEEKS
3. BYE
4. Compile time error
Output:

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?




public
class 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. 10
2. BYE
3. NO output
4. Compile time error
Output:

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?




public
class 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 GEEKS
2. Compile time error
3. WELCOME
4. 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?




public
class Test {
public
    static void main(String[] args)
    {
        if (true)
            ;
    }
}


Options:
1. No Output
2. Compile time error
3. Runtime error
4. Runtime Exception
Output:

 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 error
2. Lets work
3. Run- time error
4. Today is fun day
Output:

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?




public
class 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 Output
2. Syntax error
3. Lets have fun!
4. Compile time error
Output:

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. GEEKS
2. Compile time error
3. No Output
4. Run-time error
Output:

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. HELLO
2. No Output
3. GEEKS
4. Compile time error
Output:

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. GEEKS
2. Compile time error
3. HELLO
4. NO Output
Output:

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 error
2. GEEKS
3. HELLO
4. 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 error
2. GFG
3. No Output
4. Geeksforgeeks
Output:

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.



Last Updated : 20 Sep, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads