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?
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?
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.
If you like our products and services, please help us by posting your review here.