• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

q from Jxam

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the q,answers and some explanations:
Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
##ans1##
if ((s != null) & (s.length()>0))
##ans2##
if ((s != null) && (s.length()>0))
##ans3##
if ((s != null) | (s.length()>0))
##ans4##
if ((s != null) | | (s.length()>0))
##ans5##
None of the above.
##correct##
1,2,3,4
i checked only 1,3,4 and even I compiled and saw that 2 is not a correct answer,i have doubt about the explanation-for answer 2
"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).."
am i missing something?
rgds,
cristi
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristi,
You are missing the fact that '&&' is shortcircuit operator.
In short circuit operator evaluation, if the final result can be decided
by knowing the result of the first operand then the other operand
is NOT evaluated at all.
So the answer 2 is correct.
Hope this clears your doubt.


------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of 2nd option----the first boolean expression will be 'false' so the second part of the AND Logical operator option will not be calculated.So,There should be no Exception thrown in the 2nd option.
 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry vadiraj,u are right about theory
but here first part s!=null is avaluate to false
and false && X always evaluate false(without evaluate X)
so i think the correct answer should be 1,3,4
as golu jain observe too.
your opinion remain the same?
rgds,
cristi

Originally posted by vadiraj vd:
Hi Cristi,
You are missing the fact that '&&' is shortcircuit operator.
In short circuit operator evaluation, if the final result can be decided
by knowing the result of the first operand then the other operand
is NOT evaluated at all.
So the answer 2 is correct.
Hope this clears your doubt.


 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vadiraj vd:
(2) is NOT</> the correct answer cus in this case, s.lenth() will never be called.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic