Preview

08 - Polymorphism

 1. Polymorphism means "_________", and it occurs when we have many classes that are related to each other by inheritance.

  single forms

  multiple objects

  many forms

  single transitions

 2. Which of the following statements are true?
Which of the following statements are true?

1.Inheritance lets us inherit attributes
and methods from another class. 

2. Polymorphism uses those methods to perform 
different tasks. 

3. Polymorphism allows us to perform a single
action in different ways.

  2 only

  1 only

  All three statements are false

  All three statements are true (1,2,3)

 3. Which of the following is an example of polymorphism?

  Method Overriding

  Method Overloading

  Inner class creation

  Abstract class and sub class creation

 4. In the below code, an example of polymorphism could be how _____________________________________.
class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

  the superclass has within it a sound method that has no return type

  subclasses have their own method for sound but this is NOT given priority

  the superclass Animal overrides the subclass sounds

  subclasses have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.)

 5. The following code been implemented correctly to produce an output of: The animal makes a sound,The pig says: wee wee, The dog says: bow wow.
class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();
    Animal myPig = new Pig();
    Animal myDog = new Dog();
        
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

  FALSE

  TRUE

 6. What is the output from running the main method in the Shape class?
public class Shape {
   public void what() { System.out.print("Shape ");}

   public static void main(String[] args) {

      Shape[] shapes = {new Shape(), new Rectangle(), new Square(),
                        new Circle()};
      for (Shape s : shapes)
      {
         s.what();
         System.out.print(" ");
      }
   }

}

class Rectangle extends Shape {
   public void what() { System.out.print("Rectangle "); }
}

class Square extends Rectangle {
}

class Oval extends Shape {
   public void what() { System.out.print("Oval "); }
}

class Circle extends Oval {
   public void what() { System.out.print("Circle ");}
}

  Shape Shape Shape Shape

  Shape Rectangle Rectangle Circle

  Compile time error

  Shape Rectangle Square Circle

 7. What is the output from running the main method in the Student class?
public class Student {

   public String getFood() {
      return "Papadams";
   }

   public String getInfo()  {
     return this.getFood();
   }

   public static void main(String[] args)
   {
     Student s1 = new ComputingStudent();
     s1.getInfo();
   }
}

class ComputingStudent extends Student {

  public String getFood() {
     return "Treacle Tart";
  }

}

  Papadams

  Treacle Tart

  Compile time error

  Run time error

 8. ______________, the method in the actual object type is executed for a non-static method call. This is called polymorphism.

  In the subclasses that have been created

  During compilation

  At run-time

  At the point the object is instantiated

 9. What is the output from running the main method in the RaceCar class?
public class Car
{
  private int fuel;

  public Car() { fuel = 0; }
  public Car(int g) { fuel = g; }

  public void addFuel() { fuel++; }
  public void display() { System.out.print(fuel + " "); }

  public static void main(String[] args)
  {
     Car car = new Car(5);
     Car fastCar = new RaceCar(5);
     car.display();
     car.addFuel();
     car.display();
     fastCar.display();
     fastCar.addFuel();
     fastCar.display();
  }

}

class RaceCar extends Car
{
  public RaceCar(int g) { super(2*g); }
}

  5 6 5 6

  10 11 10 11

  The code will not compile

   5 6 10 11

 10. Analyse the following code. At compile time the declared type is Book and the Book class does not have or inherit a getDefintion method. Therefore ______________.
public class Book
{
   public String getISBN()
   {
      // implementation not shown
   }

   // constructors, fields, and other methods not shown
}

public class Dictionary extends Book
{
   public String getDefinition()
   {
      // implementation not shown
   }
}

  b.getDefintion(); would cause a compile-time error

  b.getISBN(); would cause a compile-time error

  b.getDefintion(); would not cause a compile-time error

  ((Dictionary) b).getDefinition(); would cause a compile time-error