I doubt the given answer and explanation. Please let me know whether i am right.
Question
Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
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))
i thought a, c, d will throw NPE but not b. But the given answer is a,b,c,d
explanation provided as
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)..
>>>>
Let me know whether answer 2 is right still