Page 1 of 1

Sample Questions

Posted: Mon Jun 10, 2013 3:47 pm
by deepa.patre

Code: Select all

class Feline {
public String type = "f ";
public Feline() {
System.out.print("feline ");
}
}
public class Cougar extends Feline {
public Cougar() {
System.out.print("cougar "); 
}
public static void main(String[] args) {
new Cougar().go();
}
void go() {
type = "c ";
System.out.print(this.type + super.type);
}
}


What is the result? 
A) cougar c c
B) cougar c f
C) feline cougar c c
D) feline cougar c f
E) Compilation fails
F) An exception is thrown at run time.
According to sample questions the correct answer is option c
but i feel the answer should be option d... because super.type = f and not c..

Please advise!

Re: Sample Questions

Posted: Mon Jun 10, 2013 4:27 pm
by admin
Please quote the link from where you got it.

Re: Sample Questions

Posted: Tue Jul 02, 2013 8:47 am
by deepa.patre
I got this code from OCAJP 7 sample questions. Here is the link : http://education.oracle.com/pls/web_pro ... =SQ1Z0_803

Re: Sample Questions

Posted: Tue Jul 02, 2013 8:54 am
by deepa.patre
If i change the local variable type in go() to instance variable . the output will be "feline cougar c f"... if i don't change it the output will be " feline cougar c c"... but what i don't understand is the output says "S.O.P(this.type+super.type); So how did it print c since we cannot use "this" on local variables!

Please reply