aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question" Watch "Exception question" New topic
Author

Exception question

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
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

Joined: Dec 18, 2001
Posts: 61
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.


SCSecA,SCNA,SCSA,SCWCD,SCJP
manikandan vasudevan
Greenhorn

Joined: Jan 28, 2002
Posts: 10
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

Joined: Nov 20, 2001
Posts: 47
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

Joined: Jan 17, 2002
Posts: 172

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

Joined: Jan 22, 2002
Posts: 24
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

Joined: Jan 15, 2002
Posts: 14
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

Joined: Feb 26, 2001
Posts: 4757
    
    7

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
manikandan vasudevan
Greenhorn

Joined: Jan 28, 2002
Posts: 10
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

Joined: Jan 08, 2002
Posts: 54
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.


help you means help me
manikandan vasudevan
Greenhorn

Joined: Jan 28, 2002
Posts: 10
thanks stransky and seany.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Doh! I completely missed that!
Good catch!
Junilu
 
 
subject: Exception question