Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PGJC mock exam question

 
Shaili Merchant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Meher Parveen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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 ]
 
Shaili Merchant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic