# Jxam short circuit question

Lisa Yanchunis

Greenhorn

Posts: 27

posted 16 years ago

The following is a question from Jxam:

I chose a,c,d. The correct answer on the exam is a,b,c,d with the following explaination.

This question revolves around the short-circuit logical operators (at least some of it does!).

For the following, LHS is left-hand side, RHS = right hand side.

Answer 1 must be fully evaluated. The LHS is true and so the RHS needs to be evaluated to ensure the entire expression is true. This is an AND operation, and if the LHS is true, then the RHS must be evaluated for the expression to be true.

Answer 2 is the same as above. Both sides need to be evaluated for an AND expression to be true (if the LHS is true, that is)..

Answer 3 must be fully evaluated because an OR expression is only true if one or the other is true, but not both. Therefore, since the LHS is true, the RHS must be evaluated.

Answer 4 is similar to answer 3 above.

For someone like me with 14+ years of software experience, this was a tough question! It requires that you are familiar with AND and OR truth tables, as well as Java.

I have compiled and run the code.... b DOES NOT return a NullPointerException. Why? Because he is assuming that the RHS evaluates to true.... and it evaluates to false. Since the first half returns false there is no need to evaluate the second half, the whole statement will be false no matter what with an &.

Lisa

Given the following definition:

String s = null;

Which code fragment will cause an exception of type NullPointerException to be thrown.

The answers are:

a. if ((s != null) & (s.length()>0))

b. if ((s != null) && (s.length()>0))

c. if ((s != null) | (s.length()>0))

d. if ((s != null) | | (s.length()>0))

e. None of the above.

I chose a,c,d. The correct answer on the exam is a,b,c,d with the following explaination.

This question revolves around the short-circuit logical operators (at least some of it does!).

For the following, LHS is left-hand side, RHS = right hand side.

Answer 1 must be fully evaluated. The LHS is true and so the RHS needs to be evaluated to ensure the entire expression is true. This is an AND operation, and if the LHS is true, then the RHS must be evaluated for the expression to be true.

Answer 2 is the same as above. Both sides need to be evaluated for an AND expression to be true (if the LHS is true, that is)..

Answer 3 must be fully evaluated because an OR expression is only true if one or the other is true, but not both. Therefore, since the LHS is true, the RHS must be evaluated.

Answer 4 is similar to answer 3 above.

For someone like me with 14+ years of software experience, this was a tough question! It requires that you are familiar with AND and OR truth tables, as well as Java.

I have compiled and run the code.... b DOES NOT return a NullPointerException. Why? Because he is assuming that the RHS evaluates to true.... and it evaluates to false. Since the first half returns false there is no need to evaluate the second half, the whole statement will be false no matter what with an &.

Lisa

Ravindra Gupta

Greenhorn

Posts: 3

posted 16 years ago

&& operator is evaluated both LHS and RHS only if LHS is true , if it is false it wouldn't evaluate the RHS of && operator. that's why 'b' in not throwing NullPointerException(Since LHS is false).The & has the characterstic that both LHS and RHS would be evaluated essentially. that's why 'a' throws the exception.

same happened with 'c' both side would be evaluated hence exception.

In case of | |(logical or)the RHS is evaluated onlyif LHS is false ,that is the reason in 'd' since the LHS is false it would execute RHS and throws the exception.

------------------

Cheers~

Ravindra Gupta

ravindra_gupta@123india.com

same happened with 'c' both side would be evaluated hence exception.

In case of | |(logical or)the RHS is evaluated onlyif LHS is false ,that is the reason in 'd' since the LHS is false it would execute RHS and throws the exception.

------------------

Cheers~

Ravindra Gupta

ravindra_gupta@123india.com

Cheers~<BR>Ravindra Gupta<BR>ravindra_gupta@123india.com

Ravindra Gupta

Greenhorn

Posts: 3

&& operator is evaluated both LHS and RHS only if LHS is true , if it is false it wouldn't evaluate the RHS of && operator. that's why 'b' in not throwing NullPointerException(Since LHS is false).The & has the characterstic that both LHS and RHS would be evaluated essentially. that's why 'a' throws the exception.

same happened with 'c' both side would be evaluated hence exception.

In case of | |(logical or)the RHS is evaluated onlyif LHS is false ,that is the reason in 'd' since the LHS is false it would execute RHS and throws the exception.

------------------

Cheers~

Ravindra Gupta

ravindra_gupta@123india.com

same happened with 'c' both side would be evaluated hence exception.

In case of | |(logical or)the RHS is evaluated onlyif LHS is false ,that is the reason in 'd' since the LHS is false it would execute RHS and throws the exception.

------------------

Cheers~

Ravindra Gupta

ravindra_gupta@123india.com

Cheers~<BR>Ravindra Gupta<BR>ravindra_gupta@123india.com