• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Confusion

 
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,
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

 
Vivek Shrivastava
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,
Sorry only given answer is �D�. I think I need a cup of coffee.
vivek
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 &).

Tieyi
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic