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

Exception question

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()))


Select all best answers?

A. 1
B. 2
C. 3
D. 4
Their answer: A,B,D
I really dont understand what this question is? Can someone throw some light?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A and B are not logical OR statement so it will execute both the part, you will get exception.
Where is b is logical OR statement since s == null it won't execute another one(short cricute)
C is logical OR since s is null we can't call any method on S string object, so it will throws Exception.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i know the concept of short circuit operator. but i am not clear how the given example will throw exception. Can anyone explain this example in detail.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by manikandan v:
Hi,
i know the concept of short circuit operator. but i am not clear how the given example will throw exception. Can anyone explain this example in detail.



whenever a reference variable of type String points to nothing in memory, i.e. its value is null, a call to the String method length() will throw a NullPointerException.
in this example, based on the behaviour of | and || (with short circuit expressions evaluation), the s.length() call will be made for 1,2 and 4
even though s equals null.
Hope this helps,
Bernd
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in this example, based on the behaviour of | and || (with short circuit expressions evaluation), the s.length() call will be made for 1,2 and 4
even though s equals null


That being the case, then for 1. if s!=null then the call s.length() shouldnt throw an exception since s isn't null. Corrections?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in this case, we have to say s=null first before line 1 and try to execute all the statements. Then change the value to something other than null in the second run and then try to execute all the four statements.
So, whichever statement executes with both the values of s with out throwing exception will get struck off from the answer list.
[ January 30, 2002: Message edited by: Uday Kumar ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't both of the sides of the "or" comparison have to be of boolean data type. If you say that i=s.length() - the left side of the comparison is not boolean. Wouldn't that throw a compiler error?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Salerno:
That being the case, then for 1. if s!=null then the call s.length() shouldnt throw an exception since s isn't null. Corrections?


The only safe construct is number 4. All others may cause a NullPointerException.
Since 1. uses | (full evaluation) instead of || (short-circuit), (i = s.length()) will be evaluated regardless of whether (s != null) is true or false, thus there is still a chance that it could cause a NullPointerException.
 
manikandan vasudevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Salerno:
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()))


Select all best answers?

A. 1
B. 2
C. 3
D. 4
Their answer: A,B,D
I really dont understand what this question is? Can someone throw some light?


I think the answer given is wrong.The answer should be A,B,C.
First in the RHS part of the IF condition, it should be an equality operator and not an assignment operator.If it is assingment operator then it will give the following error.
"operator | or || cannot be applied to boolean,int"
Let us take the following code using case 4:
public class abc
{
public static void main(String args[])
{
String s= null;
int i=0;
if ((s == null) || ( i == s.length()));
}
}
It will not give any exception becos of short circuit operator.
Since case 1 and 2 are not using || operator, it will throw exception when s is null.
In case 3, if s is null, it will then check the s.length(). so it will give an exception.
So A,B and C are the answers.
Am i right?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,manikandan v,I think you are right.
the operater "||" will cause short circuit,only the left pression is false,the right pression will caculate. so
4 if ((s ==null) || ( i =s.length()))
if (s == null) is false, that means s != null, it can't throw exception.otherwise, the right pression cann't be caculate.
But as concerning 3if ((s !=null) || ( i =s.length()))
if (s!= null) is false, that means (s == null),and the pression (i = s.length) will throw nullPointerException.
 
manikandan vasudevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks stransky and seany.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lubosh Bazant:
Don't both of the sides of the "or" comparison have to be of boolean data type. If you say that i=s.length() - the left side of the comparison is not boolean. Wouldn't that throw a compiler error?


Lubosh is the only one who got this right! None of the answers are correct.
Type type of s is not given, however, it MUST be an object reference type, otherwise you'll get a compiler error when you try to compare it to a null.
Try this...
int s = null; //won't work
OR
int s;
if (s == null) //won't work.
So we know s is an object type. That makes the call s.length() plausable. However, length() returns an int, and assigns this to variable i. We know that i must be an integer type. However, now that we know this, we also know an int value cannot be used in this position.
All the ifs.. start with a boolean left hand side, so all the operators are LOGICAL operators, and not bitwise operators.
However, since the right hand side is an int, and you can't compare an int to a boolean with a logical function, all of these examples will throw a compiler error.
Good job Lubosh!
Rob
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Rob and Lubosh
Paul,
where did you get this question from ?
As soon as I know that I'm gonna move this thread to mock exam errata...
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh! I completely missed that!
Good catch!
Junilu
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic