• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Q. from Jxam

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Ans given are-1,2,3,4
Explanation given by them:
((
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)..

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.

For someone like me with 14+ years of software experience, this was a tough question! It requires that you are familiar with AND and OR truth tables, as well as Java.
))

Is the explanation right?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is 1,3 and 4. 2nd will never throw an exception b'coz the part or operants before the && short circuit operator will return false and anything && false is always false so the second half will never be evaluated and hence no exception.
Keep in mind that |,& and | | will always evaluate both the operants and then will produce the final result.
I hope i am making sense here.
------------------
Cheers
Tamanna :-)
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Are you sure that the short circuit | | operator will always avaluate both operands. Surely if the left hand operator is true then it won't need to evalaute the right had side ?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Statements 1,3 and 4 will only throw the NullPointerException
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic