About Question enthuware.ocajp.i.v7.2.1177 :

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

Moderator: admin

Post Reply
elindsay
Posts: 1
Joined: Mon Dec 28, 2015 11:15 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1177 :

Post by elindsay »

Couldn't o1 be a non-null reference to a primitive (as opposed to an object), though?

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

Re: About Question enthuware.ocajp.i.v7.2.1177 :

Post by admin »

Primitives do not have references.
If you like our products and services, please help us by posting your review here.

philcave
Posts: 4
Joined: Sat Mar 05, 2016 2:15 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1177 :

Post by philcave »

Couldn't the non-null reference be that of an interface?

e.g.

MyInterface o1 = null;

if (o1 instanceof Object)
System.out.println ("true");
else
System.out.println ("false");

This would print false.

So shouldn't the correct answer be option 4 - "For any non-null reference o1, the expression (o1 instanceof Object) may yield false." ?

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

Re: About Question enthuware.ocajp.i.v7.2.1177 :

Post by admin »

philcave wrote:Couldn't the non-null reference be that of an interface?
Why do you think a reference to an interface will not be an Object?

e.g.

MyInterface o1 = null;

if (o1 instanceof Object)
System.out.println ("true");
else
System.out.println ("false");

This would print false.
Yes, it does print false. But not because o1 is not an instance of Object but because o1 is null.
Remember that every object in Java IS-A Object (because Object is the root class or all objects in Java). Therefore, o1 instanceof Object can never print false if o1 is not null.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1177 :

Post by AndaRO »

You should understand here that instanceof operator returns true even if the Right Hand Side is a super class.

So it is the statement true for a "super super class"?

I think true because the inheritance relation is transitive.

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

Re: About Question enthuware.ocajp.i.v7.2.1177 :

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests