aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification on Mind Q questions: 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 "Clarification on Mind Q questions:" Watch "Clarification on Mind Q questions:" New topic
Author

Clarification on Mind Q questions:

Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
Question no 35:
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
What will be printed to standard output?
a) The code will not compile.
b) The code compiles and "5, Super" is printed to standard output.
c) The code compiles and "5, Sub" is printed to standard output.
d) The code compiles and "2, Super" is printed to standard output.
e) The code compiles and "2, Sub" is printed to standard output.
f) The code compiles, but throws an exception.
I thought the ans is e, since we are storing child to the parent. but the correct answer is c. Can any one help me why?
------------------------------------------------------------
Question no:32
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest
{ public static void main( String[] args )
{ Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println( "Pine" );
if( tree instanceof Tree )
System.out.println( "Tree" );
if( tree instanceof Oak )
System.out.println( "Oak" );
else System.out.println( "Oops" );
}
}
Select all choices that will be printed:
a) Pine
b) Tree
c) Forest
d) Oops
e) (nothing printed)
For this question I thought a & d are the correct answer, But b is also correct. can any one help me how?
Thanks,
Bala
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
hi bala,
iam also having the same doubt . iam expecting
clarification from ajith,paul,maha anna
thanks in adv
bala
Paul A
Ranch Hand

Joined: Aug 25, 2000
Posts: 44
The concept of first question has been discussed a lot of times... anyway,
Point to remember is methods are overridden and vairables are shadowed.
Please see this post:
http://www.javaranch.com/ubb/Forum24/HTML/003599.html
Problem 2 is simple...isn't Pine a Tree too?? (Pine extends Tree)
so why should tree instanceof Tree ( where tree is actually refering to an object of class Pine) return false ?

HTH,
Paul.


------------------
http://pages.about.com/jqplus
Get Certified, Guaranteed!


<A HREF="http://pages.about.com/jqplus" TARGET=_blank rel="nofollow">http://pages.about.com/jqplus</A> <BR>Get Certified, Guaranteed!
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
hi bala,
iam also having the same doubt . iam expecting
clarification from ajith,paul,maha anna
thanks in adv
bala
bala_chocos
Ranch Hand

Joined: Aug 28, 2000
Posts: 48
hi paul
please explain in detail the answer for the second question
iam waiting for the explanations
thanks in adv
bala
Bala Raj
Ranch Hand

Joined: Sep 12, 2000
Posts: 75
Hi Paul,
For problem 2:
tree instanceof Tree is returning true. I compiled and checked.
I don't know how?
Thanks
bala
 
wood burning stoves
 
subject: Clarification on Mind Q questions: