Loading
notes intro/old submit AP Problems the dump links
 

Quiz 24 Polymorphism

 

id:

 

 

1.True or false: Polymorphism works with classes (abstract or regular) and interfaces true false

For the following examples:

public class Book
{  
   private int pages;


   //constructor initializes pages to 2000 by default
   public Book()
   {
       pages=2000;
   }

   //----------------------------------------------------------------
   //  Prints a message about the pages of this book.
   //----------------------------------------------------------------
   public void message ()
   {
      System.out.println ("Number of pages: " + pages);
   }
   
   public int getPages()
   {
       return pages;
   }
}	   

public class Dictionary extends Book
{
   private int definitions;
   
   public Dictionary(int definitions)
   {
       this.definitions=definitions;
   }
   
   //----------------------------------- ------------------------------
   //  Prints a message using both local and inherited values.
   //-----------------------------------------------------------------
   public void message ()
   {
      System.out.println ("Number of definitions: " + definitions);

      System.out.println ("Definitions per page: " + definitions/getPages());
   }

}
	    

2. Which of the following is correct?

Book b=new Dictionary(5200); Dictionary d=new Book();

 

3.In the example from class, if we declared staffList to be an array of StaffMembers and filled it as the exampe from class and staffList[0] is an executive. If we say: staffList[0].pay() would call

pay that belongs to staffMember

pay that belongs to executive

you need to cast it as an executive to use pay

 

4. Again above, but we try to say: staffList[0].bonus(300)

bonus that belongs to staffMember

bonus that belongs to executive

you get a run-time error (you should have cast it as an executive to use bonus).

you get a compile time error (you should have cast it as an executive to use bonus).

 

5. A big benefit of using polymorphism is:

it allows elegant object oriented design

it allows you to put different objects in one data structure

it allows methods to take in different objects so they are more useful

all of the above