• 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

am i correct ?

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from mock exam
Which of the following may throw an exception?
1 if ((s !=null) | ( i =s.length()))
2 if ((s ==null) | ( i =s.length()))
3 if ((s !=null) | | ( i =s.length()))
4 if ((s ==null) | | ( i =s.length()))
A. 1
B. 2
C. 3
D. 4
I think not even a single line will compile. Because u can�t use int value with �|� and �| |� operator. In given question RHS is a int type.
Feel free to correct me.

vivek
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes you are right.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, the code as written looks wrong.
But the question would become interesting
if it had used == (comparison) instead of
= (assignment) in the second argument of
each line:
1 if ((s !=null) | ( i == s.length()))
2 if ((s ==null) | ( i == s.length()))
3 if ((s !=null) | | ( i == s.length()))
4 if ((s ==null) | | ( i == s.length()))
If so, that would make each of the above
lines a comparison of two boolean
expressions.
In cases 1, 2, and 3, if s is null, a
NullPointerException would
be thrown.
However, in case 4, if s
is null, the first half of the expression
would evaluate to true, then because of the
| | shortcut operator, the second half of
the expression would be skipped, which
would allow line 4 to avoid the
NullPointerException on s.length().
 
roses are red, violets are blue. Some poems rhyme and some are a 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