Java Questions 21 - 30  «Prev  Next»

Java Questions 28

  1. When does the instance of operator return true?
    Answer:
    The instanceof operator returns true if the reference variable being tested is of the type being compared to.
  2. What is true of every class in Java?
    Answer:
    Every class in Java is a subclass of class Object (except of course the class Object).
  3. What is true about every class that you create?
    Answer:
    Whenever you create a class, you automatically inherit all of the methods in the class Object.
  4. What are the two most common reasons to use inheritance?
    Answer:
    The 2 most common reasons to use inheritance are
    1. To promote code resuse
    2. To use polymorphism.

  5. The IS-A relationships is based on ?
    Answer:
    The concept of IS-A is based on
    1. class inheritance or
    2. interface implementation
  6. Give two examples of a "IS-A Relationship"?
    Answer:
    A mustang "is a" type of horse.
    A grizzly "is a" type of bear.
  7. Answer:
    Animal is a superclass of mammal.
    Mammal is a superclass of human.
  8. How does the "IS-A" relationship relate to the extends keyword?
    Answer:
    Car extends Vehicle means Car "IS-A" vehicle.
  9. Write a class Mammal that inherits from Animal.
    public class Animal{}
    Answer:
    public class Mammal extends Animal{
    	private Lungs mammalLungs;
    }
    
  10. What test must a Java object pass in order to be considered polymorphic?
    Answer:
    Any Java object that can pass more than one IS-A test can be considered polymorphic.