File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion" Watch "Confusion" New topic


Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277

Following question is from a mock exam and there is nothing wrong with question and answer.

1 String s = null;
2 if ( s != null & s.length() > 0)
3 System.out.println("s != null & s.length() > 0");
4 if ( s != null && s.length() > 0)
5 System.out.println("s != null & s.length() > 0");
6 if ( s != null | | s.length() > 0)
7 System.out.println("s != null & s.length() > 0");
8 if ( s != null | s.length() > 0)
9 System.out.println("s != null | s.length() > 0");
Which of the following lines throwing null pointer exception
A. 2,4
B. 6,8
C. 2,4,6,8
D. 2,6,8
Given answer is 'B' and 'D'. i do agree with answer.

Should i expect these kind of answers where one answer('B') is a subset of another answer('D'). just wanna make sure.
Please do reply.

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Sorry only given answer is �D�. I think I need a cup of coffee.
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
In my opinion, only answer D is true. A subset is ambiguous if the question doesn�t specify explicitly to check all correct answers or something like that.
But take it as an opinio only please.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Well, they could present this question with radio buttons, so that you can only choose one answer. In that case, you're forced to choose the best one, which is D in this case. Of course, a better answer would be that only line 2 will throw an exception, since the others will never execute after the exception is thrown. But that just goes to show that the question is very poorly worded, and you can assume that real test questions will be less ambiguous than this one.
As far as the options are concerned, you're much more likely to see them listed as something like:
A. line 2
B. line 4
C. line 6
D. line 8
(Choose all that apply.)
This way, it's up to you to choose the correct set of options.

"I'm not back." - Bill Harding, Twister
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
why would 2 throw an exception?..the | AND bitwise operator returns true only for 1|1 now since the first part at line 2 is false ie 0 it does not have to check the second part of the if statement...i think 6,8 is the right answer..please correct me if i am wrong which i think i am since everybody seems to agree with the given answer
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sorry, Puzz,
I think you need to read your book again to find out the difference between a conditional operator (| | or &&) and a bollean operator (| or &).

I agree. Here's the link:
subject: Confusion
It's not a secret anymore!