wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PGJC mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PGJC mock exam question" Watch "PGJC mock exam question" New topic
Author

PGJC mock exam question

Shaili Merchant
Greenhorn

Joined: Nov 20, 2007
Posts: 27
Given the following class definitions, which expression identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?

class A {}
class B extends A {}
class C extends B {}
class D extends A {}

OPTIONS:
A: obj instanceof B
B: obj instanceof A && !(obj instanceof C)
C: obj instanceof B && !(obj instanceof C)
D: !(obj instanceof C || obj instanceof D)
E: !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)


Please help me to understand the question.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
The question is asking you how you can check whether obj is an object of class B (as opposed to an object of any of B's subclasses or superclasses).


SCJP 5.0
Meher Parveen
Greenhorn

Joined: Nov 25, 2007
Posts: 25
The answer has to be A. If the object obj has been instantiated using class B, it will return a true on only 2 checks, those being:
obj instanceof B
obj instanceof A

All other instanceof checks will fail anyways.

We cannot choose option B, since an object of class A will also return true for this case!

Hope this is what you wanted to know.
-Meher
Shaili Merchant
Greenhorn

Joined: Nov 20, 2007
Posts: 27
Ok..If the question is to ask how the "obj" object would be an object of class B "ONLY" then

I suppose there are 2 ways in which object B can be instantiated
1)A a=new B();
2)B b=new B();
and the third way is to create a reference of type B
3)B b=new C();

So according to me inorder for the object to be created by instantiating
class B only, the correct option should be A and B

Let me know if I am wrong..
Thanks
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Meher Parveen:
The answer has to be A. If the object obj has been instantiated using class B, it will return a true on only 2 checks, those being:
obj instanceof B
obj instanceof A

All other instanceof checks will fail anyways.

We cannot choose option B, since an object of class A will also return true for this case!


Unfortunately, that's not the right answer. Perhaps this phrasing will help you understand the question better: suppose you know that obj can be an instance of any of A, B, C, or D. How would you write an if statement whose body gets executed only if obj is a B object (and not an A, C, or D)? In other words, how would you fill in the condition below?
Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83



OPTIONS:
A: obj instanceof B

- This will return true for C as well so is incorrect

B: obj instanceof A && !(obj instanceof C)

- This will return true for A as well so is incorrect

C: obj instanceof B && !(obj instanceof C)

- CORRECT - I think

D: !(obj instanceof C || obj instanceof D)

- This will return true for A as well so is incorrect

E: !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)

- This will return false for B as well so is incorrect

Am I correct?
[ December 06, 2007: Message edited by: Cory Max ]

There are only 10 types of people in this world... Those who understand binary and those who don't.
Shaili Merchant
Greenhorn

Joined: Nov 20, 2007
Posts: 27
Alrite..Now I have finally understood the question completely and correctly.
Earlier interpretation of mine didnt consider the other alternatives...
Now I suppose the answer should be C only.
Please confirm it.
Thanks
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi All

After going through all the post
as above i think the answer is
A,B,C,D .........i dont know
if i am right or not....
may be i have misunderstood the question
please let me know.

i am realy confused with the above. :roll:

Thanks in advance.
[ December 06, 2007: Message edited by: dhwani mathur ]
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Yes, C is the correct answer. Cory posted an excellent and concise explanation by showing the counter-examples that apply to the incorrect options.

Dhwani, perhaps you aren't aware that "obj instanceof Foo" evaluates to true not only when obj is an instance of Foo, but also when it's an instance of any of Foo's subclasses. For example, if obj is a Car, then "obj instanceof Vehicle" will evaluate to true (since a Car IS-A Vehicle). That's why option E is incorrect: (obj instanceof A) will be true for B objects, so option E will reject objects of all four classes. See if the question makes sense to you now. Best of luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PGJC mock exam question
 
Similar Threads
instanceof operator
instanceof prblem help me
How to check the class of an Object given its reference?
instanceof
Doubt in instanceof operator