Menu
Topics Index
...
`

ShortCircuit Operator2797


If you need explanation Read this topic

If you need Answer Take test on this topic

ShortCircuit Operator2797
What will be the output of the following program?
public class checking
{
    public static void main(String s[])
    {
        int c = 0, e = 50;
        boolean b = (c == 1) && (++e < 100);
        System.out.println("e = " + e);
        boolean f = (++e < 100) && (c == 1);
        System.out.println("e = " + e);
    }
}

A. e = 50
e = 50
B. e = 50
e = 51
C. e = 51
e = 51
D. e = 51
e = 52
E. Compilation Error or Runtime Error
Topic: Short Circuit Logical Operators 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 B here initially c=0 and e=50  in the next statement b=(c==1)&&(++e<100) here c!=1 and ++e means e=50 here b=false and it prints e=50 and in the next f=(++e<100)&&(c==1) here e=51 c=0 and it prints e=51

Posted by Ramya Reddy Addulla    2014-11-14 08:41:24


Answer is B
In the main method, we are initialising c to 0 and e to 50
In the next step, we are doing a bitwise and operation(bitwise and of two boolean expressions is 1 only when both the expressions are 1)
(c==1) returns false and so we dont evaluate the expression right to && operator which means e is not incremented
In the SOP we are printing value of e which is 50 for the above reason.
In the next step,we are doing an other bitwise and operation,in the first expression (++e)<100 is executed which returns false and therefore e is incremented to 51
In the final step we are printing value of e as 51

Posted by Shashanka Mogaliraju    2014-11-14 08:45:11


Ans:D, initially e = 50, after that it is pre incremented then e becomes 51 whether the boolean var is T or F it doesn't matter as well as again it is pre incremented   and it e value becomes 52.

Posted by Damu Surya    2014-11-14 11:51:19


D

Posted by Jayakrishna Nani    2014-11-14 12:00:19


Ans is B...

here we have 2 local variables c and e initilized with the 0,50...

we cant use the local variables without initialization, if we do compiler will raise error

next we have boolean b...here we are using the logical and Operator...this operator will return true if and only if all the expressions is true...if one expression is false than it wont evaluate remaining expressions

in the 2nd statement c==1 is false than it wont go for the ++e<100 so that here e wont get incremented..so e value remains 50 only

next we are printing e so that is 50

next we have logical and operator here we use the pre increment so that first e wil get incremented so e is now 51 than 51<100 true so that it will enters into 2nd expression here it will false so  f will assigned with false...if in this statment if we have another expression than it wont get evaluated...

so now e is already 51 it get printed..

Posted by Uday Kumar    2014-11-14 12:36:48


ans b....Java provides two interesting Boolean operators not found in many other computer languages.
These are secondary versions of the Boolean AND and OR operators, and are known as
short-circuit logical operators.                                                                                                     Java will not bother to evaluate the right-hand operand
when the outcome of the expression can be determined by the left operand alone.Java will not bother to evaluate the right-hand operand
when the outcome of the expression can be determined by the left operand alone. This is
very useful when the right-hand operand depends on the value of the left one in order
to function properly.                                                                                                           coming to this program initialy c asssigned with 0 e with 50.                                           boolean b assigned with (c==1)&&(++e<100) it acts lik if condition.                                                     here c==1 fails and here AND used ,if both func true thn ly b true.here c==1 faild,so no need to check rightside conditions of     &&.so preincrement of e not executed.                                                                                                                      sop prints e=50                                                                                                                     f is assigned with (++e<100)&&(c==1)                                                                         here leftside of && condition true 51<100 thn it chcks rightside of && condition c==1 fails so f assigned to false.                                                                   sop prints e=51

Posted by Maheshwari Natarajan    2014-11-14 18:58:39


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. 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-17 02:08:40


© meritcampus 2019

All Rights Reserved.

Open In App