File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jxam Question

 
Thiru Narayanan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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.
A. if ((s != null) & (s.length()>0))
B. if ((s != null) && (s.length()>0))
C. if ((s != null) | (s.length()>0))
D. if ((s != null) | | (s.length()>0))
The answer is given as all the options. My understanding was that the correct answers are only A, C & D. Since the second part of option 'B' will not get executed. Can you please clarify this?
THIRU
 
Andrew zou
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done the same answer as you,
i also don't understand the answer supplied by Jxam.
B is correct, because && is short-circult operator.
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thiru,
Your understanding is absolutely correct. The correct answers are A, C and D as B is wrong becoz its short circuit operation. I tested the code, why don't you test it yourself
Jyotsna
Originally posted by Thiru Narayanan:
Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
A. if ((s != null) & (s.length()>0))
B. if ((s != null) && (s.length()>0))
C. if ((s != null) | (s.length()>0))
D. if ((s != null) | | (s.length()>0))
The answer is given as all the options. My understanding was that the correct answers are only A, C & D. Since the second part of option 'B' will not get executed. Can you please clarify this?
THIRU

 
Thiru Narayanan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jyotsna & Andrew,
I do not think there is any need to check it, as the short circuit '&&' will not execute the second part if the first part returns a 'false'
THIRU
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic