• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mock question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pine,tree and oops
geetha
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic