Computer Science 3675
Fall 2017
Practice Questions for Quiz 5

  1. True or false?

    In object-oriented programming, an object is represented by a structure holding variables and all of the methods that the object can perform.

    Answer

  2. Type inference is a process where a compiler determines the type of an expression without explicitly being told its type. Does a Java compiler perform any type inference?

    Answer

  3. What is the name of an algorithm that can solve a system of equations over a free algebra?

    Answer

  4. Given the Cinnameg type definition

      Type Frog =
          green  String
        | yellow (Integer, Integer)
      %Type
    
    Which of the following are sensible? Answer all that are sensible. (Sensible means that they compile and do not lead to failure when they run.) Treat each as a separate problem.

    1. ! kermit = frog("Kermit", 3, 4).
    2. ! kermit = green("Kermit").
    3. ! kermit = yellow("Kermit").
    4. ! kermit = yellow(3,4).
    5. Match yellow(x,y) =~ yellow(4,7).
    6. Match green(s) =~ yellow(4,7).

    Answer

  5. Using the type Frog from the preceding question, define a function size(f ) so that: if f is a Frog of the form green(name), then size(f ) is the length of name; and if f is a Frog of the form yellow(m, n) then size(f ) is m.

    Answer

  6. Suppose that you are performing type inference and encounter expression f (x), where f 's current type is α, x's current type is β and the current type of f (x) is γ. Write one equation involving α, β and γ that completely characterizes type requirements induced by this expression.

    Answer

  7. Suppose that type Tree is defined as follows in Cinnameg.

    Type Tree =
        leaf    Integer
      | nonleaf (Tree, Tree)
    %Type
    
    So a tree consists of leaves and nonleaves. Each leaf holds an integer. When we think of a tree, we think of the entire collection of leaves and nonleaves, including those in the subtrees.

    Write a definition of sum(t), which yields the sum of all of the numbers in leaves of tree t.

    Answer

  8. Using the type Tree from the preceding question, write a definition of toList(t), which yields a list of all numbers in leaves in tree t.

    Answer

  9. Repeat the previous exercise, but do not use concatenation.

    Answer

  10. Imagine that lists are not available. Write a type definition in Cinnameg of type StringList where a value of type StringList is a linked list of strings. Then do whatever is necessary to ensure that functions head, tail, nil? and operator :: are defined. Assume that :: has already been specified to be a binary operator. nil?(x) is true just when x is an empty list. Do not worry about what happens when the head or tail of an empty list is computed.

    Hint. There are two kinds of lists: empty lists and nonempty lists. What information does a nonempty list need to hold? Think of linked lists.

    Answer

  11. Parametric polymorphism (polymorphism with type variables) was not present in Java 1.1. It was added to Java with the release of Java 1.5. Why is polymorphism such an important feature of a typed language that the designers of Java felt adding it to Java was necessary?

    Answer

  12. Why is it important for a module to be able to control what it exports? Why not just export everything in the module?

    Answer

  13. Why is should a programming language provide support for modules? Can't the programmer break a program into multiple files without the programming language getting involved? Those files can be concatenated before compiling them.

    Answer

  14. 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?

    Answer