enthuware.ocajp.i.v7.-2-.1342

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

enthuware.ocajp.i.v7.-2-.1342

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1342

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

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.2.1342

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.1342

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

ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

Re: enthuware.ocajp.i.v7.-2-.1342

Post by ashishrai.kv »

sure trying hard to do it,

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests