Menu
Topics Index
...
`

If Condition


If you need explanation Read this topic

If you need Answer Take test on this topic

If Condition
What will be the output of the following program?
public class Cricket {
    static boolean ball;
    public static void main(String[] args) {
        short bat = 42;
        if (bat < 50 & !ball)
            bat++;
        if (bat > 50)
            ;
        else if (bat > 40) {
            bat += 7;
            bat++;
        }
        else
            --bat;
        System.out.println(bat);
    }
}

A. 41
B. 42
C. 50
D. 51
E. Compilation Error or Runtime Error
Topic: Static Keyword 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.

ans is D

Posted by Haritha Damarla    2013-06-27 03:51:05


because : it first enters into first if condition and value becomes 43 and next if condition gets terminated and goes to else if condition and it becomes 43+7 = 51

Posted by Haritha Damarla    2013-06-27 04:08:27


43+7=50 and bat++=50+1 = 51

Posted by Haritha Damarla    2013-06-27 04:14:14


d

Posted by Shruthi Shru    2013-06-27 05:13:51


Option D it must be ! :) since bat<50 n ball is false 1st condition is true and bat is incrementd to 43, the 2nd condition l be false, the 3rd is true again,it will be 43+7=50 dn 51 !! Thus 51 it must print as output.

Posted by Anchal Shetty    2013-06-27 05:27:17


Congratulations Anchal Shetty. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-06-27 11:05:59


© meritcampus 2019

All Rights Reserved.

Open In App