Given the following definition: String s = null; Which code fragment will cause an exception of type NullPointerException to be thrown. 1.if ((s != null) & (s.length()>0)) 2.if ((s != null) && (s.length()>0)) 3.if ((s != null) | (s.length()>0)) 4.if ((s != null) || (s.length()>0)) 5.None of the above. Answer given are 1,2,3,4 Explaination for answers are given below. 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. Correct me if i am wrong here, The explanation is correct for the answer 3 because | is not a short circuit operator hence both the expressions will be evaluated but answer 4, the || is a short circuit operator and once the first expression is true it should not evaluate the other expression because true OR false is always true and not as explained above which says true OR true is false. and further true XOR true is false Can anyone explain to me about this Thanks in advance Kareem

For |, the result value is false if both operand values are false; otherwise, the result is true.

It sounds to me that the explanation for number 3 is an explanation of an XOR operator, ^, even though the answer uses an OR operator, |. Therefore, I believe the correct answer should be 1, 2, and 3, but not 4. Someone please corret me if I'm overlooking something. Corey

4 will also throw a NullPointerException because the left operand is false and thus the right operand has to be evaluated. Since s is null s.length() will throw a NullPointerException

I agree with Mc but i wrote this test class and it gives me null pointer exception. class Test{ public static void main(String [] s1){ Test t = new Test(); String s = null; if((s != null) || (s.length() > 0)) System.out.println("Hello"); else System.out.println("Bye"); } } I am confused Thanks in advance Kareem

Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102

posted

0

Thanks val it was pretty silly to miss that first expression itself is false

Originally posted by Kareem Qureshi: Given the following definition: String s = null; Which code fragment will cause an exception of type NullPointerException to be thrown. 1.if ((s != null) & (s.length()>0)) 2.if ((s != null) && (s.length()>0)) 3.if ((s != null) | (s.length()>0)) 4.if ((s != null) || (s.length()>0)) 5.None of the above. Answer given are 1,2,3,4 Explaination for answers are given below. 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 1 is correct

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)..

This must be where the author started smoking crack. NO,both sides do not need to be evaluated. && is a short-circuit operator, meaning it will not evaluate the RHS operator if it doesn't need to. Since s == null, s!=null is FALSE, so the entire statement is FALSE, and the RHS is not evaluated. Answer 2 is legal code that will not throw an exception

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.

Like Corey mentioned, this seems to be describing exclusive or (^) and not or (|). This supports my crack-smoking theory.

Answer 4 is similar to answer 3 above.

. More crack. Although it is true that both sides have to be evaluated, since the LHS is false, but the RHS could be true...but it will in fact throw a NullPointerException because s is null.

So my answers are 1,3, and 4, since they all throw NullPointerExceptions, because the RHS tries to invoke a method on a null reference. 2 is not a correct answer, because it will NOT throw a NullPointerException, since it uses a short-circuit && operator, and the RHS is never evaluated. [ March 19, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4

Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102

posted

0

Still i feel that explaination given is wrong. Kareem

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

posted

0

Oops! Thanks, Val. I misread. Indeed, the fourth one will throw a NullPointerException due to the fact that the left hand operand is false. The variable s is null so (s != null) will return false. Therefore, the right hand side is also evaluated. However, I still don't agree with the explination of 3 because it sounds like they're talking about XOR, rather than OR. Corey

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

posted

0

with 3 the bottom line is that it uses non-short-circuit operator and that both operands will be evaluated, thus since s is null the RHS will throw a NullPointerException As for 2, Rob is right, 2 is not a correct answer since the RHS will not be evaluated

Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102

posted

0

Rob you are right because && is short circuit operator so the 2 expression is not even reached so no nullpointer exception (more errors in exam) Kareem

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.