Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String??? 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 "String???" Watch "String???" New topic


Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
I have doubts regarding the following question:
Q) Which of the following may throw an Exception:
(a) if((s != null) | ( i=s.length()))
(b) if((s == null) | ( i=s.length()))
(c) if((s != null) | | ( i= s.length()))
(d) if((s == null) | | ( i=s.length()))
The options are:
1) a
2) b
3) c
4) d
My answer was 1, 2 & 3
Their answer was 1, 2 & 4
There are 2 things that I would like to point out:
1) I tried compiling the above statements but the line i=s.length() was not acceptable to the compiler and it said "Cant convert int to boolean". Are all of the above mentioned statements incorrect ?? i.e. can we have an assignment as a if() condition.
Also Genrally if i have the following code will it compile,
int a=5;
int b=5;
I recollect that I have read somewhere that assignments always return a boolean (true if successfull and false if not successfull) . In that case I should not get the above mentioned error.

2) Also if we can have a assignment in the if () condition then, also C could throw a NullPointerException if s=null.
Pls clarify ....i am confused.

sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
Hi there,
as XXX in if( XXX ) must evaluate to boolean,
int a=5;
int b=5;
this won't work. this works in C++ though. that's probably where you got confused.
and as for the Question itself.. I think that is wrongly written and what it is trying to test is whether you know the difference between | and | | .. assuming I am right, the answer should be all of them. what do you think?

Sean <br />SCJP2, SCJP2p1.4, SCWCD
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
a=b is an assignment, to compare a with b you need to use a==b.
The question is testing your knowledge of | and | |.
The problem is that if s is null and you try to evaluate s.length() a nullpointerexception will be thrown.
With | both operands will be evaluated under all circumstances so (a) and (b) may throw exceptions.
With | | the right operand will only be evaluated if the left operand evaluates to true, so (c) will check the length only if s is not null, therefore won't throw an exception.
(d) will check the length if s is null and will always throw an exception
The correct answer is therefore 1,2 and 4
Peter Voorwinden

Joined: Aug 02, 2000
Posts: 8
I think you're wrong in your explanation of | |. In case of operator | |, if the left operand evaluates to true, the complete expression will be true, no matter what the right operand will be. So there's no need to check the right operand (and it won't be evaluated!). In case of operator && both operands need to be true for the complete expression to be true. So if the left operand is true, it still needs to evaluate the right operand to be able to determine the value of the complete expression.
So in case of answer 3: if s == null, then the left operand evaluates to false, so the right operand will be evaluated and will result in a NullPointerException.
In case of answer 4: if s == null, the left operand evaluates to true and the right operand will not be evaluated.
So I think the answer should be 1, 2 and 3
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
I am convinced by your argument, I bow to your superior knowledge
[This message has been edited by Graeme Brown (edited October 25, 2000).]
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 104
Peter, I agree with you - 1,2,3 might cause an exception.
Just to add:-
Using && means that if you check the first operand and it's false, you don't have to go on and check the second one because the whole expression must be false whereas & checks both operands anyway.
I agree. Here's the link:
subject: String???
jQuery in Action, 3rd edition