• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String???

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
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;
if(a=b){}
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.

Sagar
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
as XXX in if( XXX ) must evaluate to boolean,
int a=5;
int b=5;
if(a=b){}
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?
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greame,
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
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am convinced by your argument, I bow to your superior knowledge
[This message has been edited by Graeme Brown (edited October 25, 2000).]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Kathy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic