• 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

Short Circuit Operators

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here was the question:
1 if ((s !=null) | ( i =s.length()))
2 if ((s ==null) | ( i =s.length()))
3 if ((s !=null) || ( i =s.length()))
4 if ((s ==null) || ( i =s.length()))

Which may throw an exception.
The answer is 1,2,4, It's 1,2,3 right?
I mean if s == null, then 3 would be (false) || (exception)...somebody confirm my sanity please.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mui,
Yes, 1 2 3 will throw NullPointerException. but you need to change i=s.length() to something
like s.length()>0 .. so it evalue to boolean on the right side of | or || operator
HTH
[ May 07, 2003: Message edited by: chi Lin ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good point chi. None of these statements would even compile, nevermind throw exceptions. Not a very good mock exam question
I really wish that all people who set mock questions would at least compile and run their code before they publish the questions. Otherwise it just causes mayhem and confusion.
 
Honk if you love justice! And honk twice for tiny ads!
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