Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification on Mind Q questions:

 
Bala Raj
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bala,
iam also having the same doubt . iam expecting
clarification from ajith,paul,maha anna
thanks in adv
bala
 
Paul A
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
bala_chocos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi bala,
iam also having the same doubt . iam expecting
clarification from ajith,paul,maha anna
thanks in adv
bala
 
bala_chocos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
For problem 2:
tree instanceof Tree is returning true. I compiled and checked.
I don't know how?
Thanks
bala
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic