Menu
Topics Index
...
`

If else if ladder output


If you need explanation Read this topic

If you need Answer Take test on this topic

If else if ladder output
What will be the output of the following program?
class StudentPass
{
    public static void main(String s[])
    {
        int marks = 80;

        if( marks > 70 )
        {
            System.out.println("Distinction");
            System.out.println("Congratulations");
        }
        else if( marks > 35 )
        {
            System.out.println("Pass");
        }
        else
        {
            System.out.println("Fail");
            System.out.println("Better luck next time");
        }
    }
}

A. Pass
B. Distinction
Congratulations
Pass
C. Distinction
Congratulations
D. Distinction
E. Compilation Errors
Topic: if else if ladder In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is C..Since the value of marks is 80 which is greater than 70, if block is executed and else if and else blocks  are skipped

Posted by Shashanka Mogaliraju    2014-11-20 08:48:38


C

Posted by Dereje Goshu    2014-11-20 08:58:48


The ans iz c
Bcoz marks are 80 and d frst condition is checking whether marks are greater than 80 or not
Coz its true the output will be c
Condition s further won't be checked coz after 1$t condition becomes true no other condition is applicable

Posted by Rohit Hrthaker    2014-11-20 09:40:55


The ans Is C.. its becoz the first condition if(marks>70) is true, therefore the respective lines will be executed and the control will skip the else clauses.. further conditions wont be checked..it is the property of if-else if-else statements..

Posted by Shankha Bagchi    2014-11-20 11:06:56


ans c. this program is based upon a sequence of nested ifs is the
if-else-if ladder.The if statements are executed from the top down. As soon as one of the conditions controlling
the if is true, the statement associated with that if is executed, and the rest of the ladder is
bypassed. If none of the conditions is true, then the final else statement will be executed.
The final else acts as a default condition; that is, if all other conditional tests fail, then thelast else statement is performed. If there is no final else and all other conditions are false,
then no action will take place. in this program int variabke marks is assigned with 80. here 1st if condition checks marks value is greaterthan 70 or not,if value greaterthn 70 then if statement get executed,if value lessthan or equal to 70 ,thn control goes to else if part.here value 80 greater than 70 .so 2 sop get printd. final else is act as default,if all the if and else if fails thn ly it get executed. function of this if -else-if ladder similar to switch case

Posted by Maheshwari Natarajan    2014-11-20 17:53:14


Answer is C...

Here we have the concept of if-else-if ladder.. The concept of if-else-if ladder is 'if' statement gets executed when the condition is true or satisfies the condition, and remaining else part can be ignored. If the condition is false then the final else statement can be executed. let us consider with example :

if( condition1 )
{
statement1;
}
else if( condition2 )
{
statement2;
}
else if( condition3 )
{
statement3;
}
else
{
statement4;
}

if the condition1 is true, Then only statement1 wil be executed, if condition1 is false and condition2 is true then only statement2 is.executed. if condition1 And condition2 are false, and condition3 is true then only statement3 is executed. finally if all the conditions are false then else statement4 can be executed.

Coming to the program, int value 80 is assigned to marks, the first condition if(marks > 70) is gets satisfies. So that two display statements gets executed. i.e., "Distinction" , "Congratulations". And the remaining part can be ignored. If supppose if condition become false i.e., <70 then the else part have been executed.. so finally it prints the required output

Posted by Sai Ram    2014-11-20 19:19:18


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-21 02:11:23


© meritcampus 2019

All Rights Reserved.

Open In App