Computer Science 3675
Summer 2003
Solutions to the practice questions for quiz 5

  1. Write a clearly legible T to the left of each of the following that is true, and a clearly legible F to the left of each that is false.

    1. In a typeless language, all type checking done at run time. T

    2. Standard ML is a strongly typed language. Therefore, in Standard ML, it is not possible to get a type error at run time. T

    3. Polymorphism is most useful in designing custom applications, and is rarely used in function libraries. F

    4. C++ uses name equivalence of types defined using typedef. F

    5. Programs written in an object-oriented style tend to be organized significantly differently from programs written in a functional or procedural style. T

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

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

  2. Types and functions can both be viewed as kinds of encapsulations. A function encapsulates an algorithm for solving a problem; it hides the details of the algorithm. What is the main information that a fully abstract type encapsulates?

    A fully abstract type encapsulates (and hides) how the information is representated.

  3. Why do most modern languages employ name equivalence instead of structural equivalence for types?

    Name equivalence allows for stricter type checking, so that a compiler can detect and report some common errors.

  4. Polymorphism without type variables typically requires that a programmer defeat the compile-time type system. Give an example where the type system would need to be defeated without type variables, but where type variables would allow the compiler to perform type checking. (A programmer defeats the type system by providing type information that is different from what the compiler would infer, such as in doing a cast; the compiler is expected to go along with this information without asking any questions.)

    Let f be the identity function, f(x) = x. Its type if ANYTHING -> ANYTHING. If you say y = f(x), then the programmer knows that y and x have the same type, but the compiler does not know that. So the programmer must defeat the type system, telling the compiler to treat y as having the same type as x. For example, a program might say

        int x;
        ...
        y = (int)(f(x));
    
    telling the compiler information (that f(x) should have type int) that it cannot glean from the type of f. Type variables would allow the compiler to infer that y must have the same type as x.

  5. What is the most general polymorphic type of function f defined by f(x) = tail(right(x))? Use type variables. Function right takes the right-hand member of an ordered pair. (Try an example. What kind of thing makes sense?)

    f: (A,[B]) -> [B]
    where A and B are type variables.

  6. What is the most general polymorphic type of the Astarte function filter? Use type variables. Filter is defined so that (filter p L) returns a list of all members x of list L such that p(x) is true. For example, if function positive returns true on a positive number and false on a nonpositive number, then (filter positive [9,-2,-3,6]) = [9,6]. Notice that p is a predicate.

    filter: (A -> boolean) -> ([A] -> [A])
    where A is a type variable.

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

    In object-based programming, a private data field (variable) is accessible only to the object that contains it. In object-oriented programming, it is accessible to that object and to all other objects of the same class.

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

    An object carries a tag indicating its class. Functions are stored according to their class in a dispatch table. The part of the run-time support that locates functions in the dispatch table is called the dispatcher.

  9. Variables are found by means of selectors. 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 subclasses of a class? How do the selector(s) work?

    Variables are found by their position in the block of memory that represents an object's state. They are found at the same position regardless of the class of the object. So a single implementation works for all subclasses of the class where the variable was created.

  10. What is an abstract class?

    An abstract class is a class with at least one virtual functions. You are not allowed to create an instance of an abstract class unless that instance is actually an instance of a subclass of the abstract class.

  11. What are the characteristics of a virtual functions? What makes it virtual?

    A virtual function logically belongs to a class that is higher in the class hierarchy than the level at which function can be implemented. It is declared to belong to the higher level, but implemented at the lower levels.