Hi, 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. regards vivek
Joined: Jun 03, 2000
Hi, Sorry only given answer is �D�. I think I need a cup of coffee. vivek
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.
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
Joined: Nov 22, 2008
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 &).