Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

mock question  RSS feed

 
bala_chocos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys
please answer this ques and explain
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");
}
}
What will be the output?
x a) Pine
x b) Tree
c) Forest
x d) Oops
e) (nothing printed)
 
geetha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pine,tree and oops
geetha
 
Pragya Prakash
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pine
Tree
Oops
This is because, the instanceof operator returns true if the class of the left-hand argument is the same as, or is some subclass of, the class specified by the right-hand operand.
The instanceof operator tests the class of an object at runtime. The left-hand argument can be any object reference expression, usually a variable or an array type, while the right-hand operand must be a class, interface, or array type.
Regards,
Pragya
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!