This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Mock Exam Errata and the fly likes Jxam Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Jxam Question" Watch "Jxam Question" New topic
Author

Jxam Question

Thiru Narayanan
Greenhorn

Joined: Apr 30, 2001
Posts: 23
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

Joined: May 31, 2001
Posts: 6
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

Joined: May 09, 2001
Posts: 94
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

Joined: Apr 30, 2001
Posts: 23
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
 
subject: Jxam Question
 
Similar Threads
Operators
Confusion
NullPointerException
Exception
Mock Exam Question.Please anybody help me.