aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Good practice question from .VoodooExam Guess o/p Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Good practice question from .VoodooExam Guess o/p" Watch "Good practice question from .VoodooExam Guess o/p" New topic
Author

Good practice question from .VoodooExam Guess o/p

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
hi friends,
this is a good one , guess the o/p. good luck,


Options :
a . 2
b . Compile error " No method matching get ( ) found in class Q026 . "
c . 5
d . NullPointerException thrown at run-time .
But the answer is none of these, can u get guess what it is
good luck


"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
B's get method
6
is this correct?


SCJP 1.4<br />SCWCD 1.4
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

6


Groovy
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
Hi Vishy,
Tha ans should be "B's get method" and 6
Here is how 6 is printed.
1)The line Q028 obj = new Q028();invokes the no-args constructor of the class B, which in turn invkes the no args constructor of A So i is incremented twice and the value is now '2'.
2) A ob =new A();This invokes the no-args constructor of A so, i is '3'.
3)Q028 invokes the getmehtod() of B. which returns (i+3) which is 6 now.
I hope I am right.


Thanks,<br />Lakshmi.
Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hi Guyz,
U all r right, actually missed one thing in the question, actually the output still remains the same.
Q028 obj = new Q028();
A ob = new A();
ob = (A)obj; //----------this is the change.
System.out.println(ob.get());
Now I want to confirm with u all ,the casting with (A)obj still call the get method of class B as Q028 extends B right ?? do explain
thanks,
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Polymorphism is in play at ob = obj. ob which is of type A is assigned a subclass of its type Q028. The get() method is overridden in B and inherited in Q028. So which get() method is called will depend on the object's type at runtime which, according to your code, is Q028.
 
jQuery in Action, 2nd edition
 
subject: Good practice question from .VoodooExam Guess o/p