INTERPRET THE PROGRAM AND WRITE THE OUTPUT, THEN WRITE THE PROGRAM AND EXECUTE TO VERIFY IT. IF YOUR ANSWER WAS WRONG THEN UNDERSTAND WHY IT IS AND WRITE A BRIEF NOTE ABOUT HOW PROGRAM IS WORKING.

ANSWER :

PROGRAM :  1

In the below program variable y is declared outside the if body but initialized inside the body.So the output would be :

10

20

20

class Else1{
    public static void main(String[] felight){
        int x=10;
        int y;//not initialized
        if(x==10){ //condition true
            y=20; //initialized y inside if block
            System.out.println(x);
            System.out.println(y);
        } else {
            y=30;
            System.out.println(x);
            System.out.println(y);
        }
        System.out.println(y);
        //accessing value of variable y 
        //from outside of if block
    }
}
/*
D:\Felight\Basic Java>javac Else1.java

D:\Felight\Basic Java>java Else1
10
20
20
*/

 

PROGRAM :  2

In the below program the output will be :

I am batman

class Else2{
    public static void main(String[] felight){
        int i=0;
        if(i++ == 0) // condition true 
            //first compared then increased
            System.out.println("I am Batman");
            //only the above statement will execute
        else if(i++ == 1)
            System.out.println("I am Spierman");
        else
            System.out.println("I am Me");
    }
}
/*
D:\Felight\Basic Java>javac Else2.java

D:\Felight\Basic Java>java Else2
I am Batman
*/

 

PROGRAM :  3

In the below program the first if expression is false and second is true.So the output will be :

Masalapuri

class Else3{
    public static void main(String[] felight){
        int i =10;
        if(i==10-1)//condition false
            System.out.println("Panipuri");
        else if(i==10)//condition true
            System.out.println("Masalapuri");
            //the above statement will execute
        else
            System.out.println("Ompuri");
    }
}
/*
D:\Felight\Basic Java>javac Else3.java

D:\Felight\Basic Java>java Else3
Masalapuri
*/

 

PROGRAM :  4

In the below program second if expression is true.So the output will be :

Masalapuri

class Else4{
    public static void main(String[] felight){
        int i=10;
        if(i != 10){}//condition false 
        // an empty if body
        else if(i==10)//condition true
            System.out.println("Masalapuri");
        else
            System.out.println("Ompuri");
    }
}
/*
D:\Felight\Basic Java>javac Else4.java

D:\Felight\Basic Java>java Else4
Masalapuri
*/

 

PROGRAM :  5

In the below program compiler will give an error because the else statement is not attached with any if statement ,its representing the body of if statement.Else statement is not valid without if statement.So its will be a compiler error.

class Else5{
    public static void main(String[] felight){
        int i=10;
        if(i != 10)//false
         else //this else is the body of previous if statement
        // it will give an error
        //because its not attached with any if statement
        System.out.println("Ompuri");
    }
}
/*

D:\Felight\Basic Java>javac Else5.java
Else5.java:5: error: 'else' without 'if'
                        else //this else is the body of previous if statement
                        ^
1 error

*/

 

PROGRAM :  6

In the below program the if expression is not valid because it returning a integer value 20 ,not a boolean value.Conditional expression always return boolean value (true or false) . So the compiler will give an error.

 

class Else6{
    public static void main(String[] felight){
        int i=10;
        if(i+10)//this is not a valid if expression
            System.out.println("I am Super Star");
        else
            System.out.println("I am not star..Just huamn");
    }
}

/*
D:\Felight\Basic Java>javac Else6.java
Else6.java:4: error: incompatible types: int cannot be converted to boolean
                if(i+10)//this is not a condition
                    ^
1 error
*/