• 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

ibm test question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one please explain me which one is the valid statement & will compile fine.
a)String s = null; boolean y = true; if(y | | s.length()>0){}

b)String s = null; boolean y = false; if(y && s.length()>0){}

c)String s = null; boolean y = true; if(y && s.length()>0){}

d)String s = null; boolean y = false; if(y | | s.length()>0){}
Thanks in advance
Ketu
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the answer is b,c. Becoz,these two cases there is no chance for 'Null POinter Exception' to be thrown.
Hope I am right.
Thanks
Nirmala

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think only answer is 'B', because in case of 'C' it will throw a null pointer exception (value of y is true).
please feel free to correct me.
vivek
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ketu,
All of them will compile fine!!. However, when you run the program, only (a), (b) and (d) will run fine, the rest will throw a NullPointerException.
This question deals with the short-circuit logical operators. For an && expression, if the first operand evaluates to false, the second operand is never evaluated.
Similarly for an | | expression, if the first operand evaluates to true, the second operand is never evaluated.
Hope this helps,
Ajith
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajith,
Correct me if I am wrong.... D will also throw the NullPointer Exception
As u said :
for an | | expression, if the first operand evaluates to true, the second operand is never evaluated.
Then how abt d....? u said a, b, d will run fine. But The following throws the exception.
d)String s = null; boolean y = false; if(y | | s.length()>0){}
Comments r appreciated.
Aruna.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ketu,
Everything will compile fine but at runtime, only a and b will run properly, but with c and d NullPointerException will occur.
Ajith's answer is correct, but I think he wrote d will run fine by mistake.
Kiran
 
K2 Joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everybody, so the final answer is all will compile but only a& b will run fine.
ketu
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have a doubt. If a ques. says which of these is a valid statement. What does it mean?
a) It compiles as well as runs fine
b) It compiles but doesn't matter whether it runs fine or not.
Thanks
KN
reply
    Bookmark Topic Watch Topic
  • New Topic