Computer Science 3675
Fall 2006
Solutions to additional practice questions for final exam

  1. True/False.

    1. The class is an important concept of object-based programming.

      False. Object-based programming works just with objects; it does not need classes.

    2. In most object-oriented languages, some type checking must be done at run time.

      True. You need to be able to do "downcasts", where, you move a type down the class hierarchy, and that requires a run-time type check. For example, you might have an object of class Object which you think is a special kind of object, of class Kangaroo. So you do a downcast, checking that it really is a kangaroo.

  2. In object-oriented programming, is a private variable of an object accessible only to that one object, or is it possible for other objects to access it directly?

    No. Private components of an object are also visible to other objects of the same class.

  3. A base class of a class A is a class that A immediately extends. In a single-inheritance object-oriented language, is there a limit on the number of base classes that a class can have?

    Yes. In a single-inheritance language, a class can only have one base class. The hierarchy diagram is a tree.

  4. In a single-inheritance object-oriented language, is there a limit on the number of subclasses that a class can have?

    No. A class can have any number of children in the class hierarchy, but can only have one parent if the language is single-inheritance.

  5. In object-oriented programming, you imagine that objects carry methods with them. Yet, the methods are not really stored with the objects. How does an object locate its methods? How does it know which methods to select? What is the name of the system support that is responsible for locating methods?

    Each object has a tag indicating its class. To find a method, you use the class and the method name (or number) to look up the method in the dispatch table. The software that does this lookup is usually called the dispatcher.

  6. Some object-oriented languages allow selection of methods to be done either statically or dynamically, depending on how the method is defined. What is the difference between static and dynamic selection?

    When you select a method from the dispatch table, you need to give a class and a method number. When you do static selection, you choose the class at compile time. When you do dynamic selection, the class is obtained at run time by looking at the tag of a object.

    Most object-oriented languages do dynamic selection almost exclusively. (An exception is the selection of methods from a superclass (using the super object); the superclass is chosen statically, and does not depend on the tag of an object. We did not talk about this.)

    C++ is an unusual object-oriented language. The default method selection is static; you only get dynamic selection for methods that are marked virtual.

  7. How does the mechanism for inheriting variables work in single-inheritance object-oriented languages? Is there a separate implementation of each selector for each class, or does one implementation of each selector work for all classes? How do the selector(s) work?

    Variables are inherited by knowing their positions. A variable is put at the same position in a structure in a class and all of its subclasses. This only works in a single-inheritance language.

  8. What are the characteristics of a virtual method? What makes it virtual?

    A virtual method is declared for a class, and is intended to be polymorphic over all of that class's subclasses. But each subclass needs to provide its own definition of the virtual method.

    Nonvirtual methods are defined by parameteric polymorphism, where one defintion works for a class and all of its subclasses. Virtual methods are defined by ad-hoc polymorphism, where each subclass needs to provide its own definition.

  9. What is an abstract class?

    An abstract class is a class that has at least one virtual method. Since you do not know how to define the virtual method for this class, you cannot create any objects that are only members of this class. You can only create objects that are instances of subclasses of an abstract class.