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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

this question is very ambiguous!!!

You're giving us options to choose from based on:
A a = null;
AA aa = null;

No where in the question says that not even a hint:

A = new AA();

i suggest you rephrase this question.

Thanks.

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

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

Post by admin »

There is no hint required to answer the question. The question is not ambiguous at all.

HTH,
Paul.

cblanche
Posts: 1
Joined: Wed Mar 27, 2013 4:10 am
Contact:

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

Post by cblanche »

The question is not ambigous but maybe the explanation could be improved.

((AA)a).doStuff(); will compile as this code do :
String s = null;
s.toString();
and throw a NullPointerException. If the object a is instanciated then we will have a ClassCastException.

You did a very good job for most of the explanation, thank you.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post by Kevin_C »

I had the answer correct, but I have a question above the explanation of the last option. This was the question:

Code: Select all

class A{ public int getCode(){ return 2; }
class AA extends A{ public void doStuff(){ } }
// Given the following two declarations, which of the options will compile?
A a = null;
AA aa = null;
Last option:

Code: Select all

((AA)a).doStuff();
With the explanation: "Once you cast a to AA, you can call methods defined in AA. Of course, if a does not point to an object of class AA at runtime, a ClassCastException will be thrown."

My question is: In this particular case will this option throw a ClassCastException or a NullPointerException (since a is null)? My guess is a ClassCastException since a doesn't point to an object of class AA (but instead it points to null), but I'm not sure.

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

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

Post by admin »

In this particular case it will throw NPE because a null can be cast as any class. Since a points to null, it can be cast as AA. So no CCE.

HTH,
Paul.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post by Kevin_C »

Ah ok, thanks for the answer. Maybe that should be added to the answer. It isn't really relevant to the question itself, but it's still good to know that it will throw a NullPointerException in this case, instead of a ClassCastException.

Something like: "Once you cast a to AA, you can call methods defined in AA. Of course, if a does not point to an object of class AA at runtime, a ClassCastException will be thrown.
In this case however, a is null, so there won't be a problem with the casting (since an AA-object can also be null). Instead, it will throw a NullPointerException."

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

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

Post by admin »

Added.
Thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests