aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jxam question 30 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jxam question 30" Watch "Jxam question 30" New topic
Author

Jxam question 30

Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
This doesn't seem right to me. If you check out §15.24 Conditional-Or Operator || in the JLS, you'll find the following text:

The || operator is like | (�15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false.

Going back to the referenced section, §15.22.2 Boolean Logical Operators &, ^, and |, you find the following text:

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


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
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
Thanks val it was pretty silly to miss that first expression itself is false
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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
Still i feel that explaination given is wrong.
Kareem
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jxam question 30