Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception question

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • 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?
 
sun ram
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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.
 
manikandan vasudevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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.
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Uday Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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 ]
 
Lubosh Bazant
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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?
 
Junilu Lacar
Bartender
Posts: 7333
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Seany Iris
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks stransky and seany.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • 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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 7333
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! I completely missed that!
Good catch!
Junilu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic