Page 1 of 1
enthuware.ocajp.i.v7.-2-.1342
Posted: Thu Mar 22, 2018 8:25 am
by ashishrai.kv
Code: Select all
Consider that str is a local variable of class java.lang.String. Which of the following lines of code may throw a NullPointerException in certain situations?
1-if ( (str != null) | ( i == str.length() ) )
2-if ( (str == null) | ( i == str.length() ) )
3-if ( (str != null) || (i == str.length() ) )
4-if ( (str == null) || (i == str.length() ) )
why option 4 is not correct as (Str==null) return true and that means it will throw an NullPointerException?
Re: enthuware.ocajp.i.v7.2.1342
Posted: Thu Mar 22, 2018 8:33 am
by admin
Why do you think Str==null should throw a NPE? A NPE is thrown when you try to access the object using null.
Re: enthuware.ocajp.i.v7.2.1342
Posted: Thu Mar 22, 2018 8:41 am
by ashishrai.kv
correct me if i am wrong that means if i will try to access STR with some object then it may throw a null pointer exception?
can you give me an small example?
Re: enthuware.ocajp.i.v7.2.1342
Posted: Thu Mar 22, 2018 9:21 am
by admin
You access an object through a reference by applying a the dot operator on it. For example, str.length(). str is the reference, then you have a dot and then the member of the object that you are trying to access.
This is quite basic. You need to go through a good book before attempting mock exams.
Re: enthuware.ocajp.i.v7.-2-.1342
Posted: Thu Mar 22, 2018 4:09 pm
by ashishrai.kv
sure trying hard to do it,