Computer Science 3675
Summer 2000
Practice questions for final exam

The final exam will be closed book. You will have 90 minutes. Many of the questions will be true/false or multiple-choice. For this practice test, the questions are short essay questions instead.

  1. What is a declarative program? What is an imperative program?

    A declarative program is a collection of facts. An imperative program is a collection of commands (ordered, or structured, in some way).

  2. Explain what a garbage collector does.

    A garbage collector finds memory that is not in use by a program and returns it to the pool of free memory.

  3. What are some advantages of interpreters over compilers? Of compilers over interpreters?

    Interpreters are more portable then compilers, and are often easier to write. Debugging tends to be easier with interpreters. Compilers produce much faster executable code, and can be chained (doing multiple translations). A compiled language can be implemented in itself. (The first Pascal compiler was written in Pascal.)

  4. How does the lvalue of a variable differ from the rvalue of the variable?

    The lvalue is the variable itself, while the rvalue is the content of the variable.

  5. What is the main difference between object-based programming and object-oriented programming.

    In object-based programming, the fundamental concept is that of an object. Objects represent the unit of encapsulation. In object-oriented programming, the fundamental concept and unit of encapsulation is a class.

    In object-based programming, each object is build in a custom way. In object-oriented programming, objects are built according to the plans of a class, and many identical objects can be stamped out from the same mold.

  6. A prolog interpreter is a theorem prover. What kind of proofs does it do?

    It does constructive existence proofs.

  7. What is the form of axioms that are allowed in a logic program?

    The axioms must be Horn clauses.

  8. What is the difference between a lexeme and a token?

    A lexeme is a sequence of characters that stands for a token. A token is a concept of the language. For example, C++ has a token called if. Its lexeme is the string "if".

  9. What list representation is good for performing the :: operator efficiently? What list representation is good for performing indexing efficiently?

    For :: a good representation is the linked representation. For indexing, sequential representation is better.

  10. Using map and lfold and possibly a helper function, write an expression whose value is 0 + 2 + 4 + 8 + ... + n.

    double(x) = 2*x. Answer: lfold(0,+) (map double (0 upto n/2))

    (I have assumed that n is even.)

  11. What is the most general polymorphic type of function f defined by f(x,y) = y::tail(x)? This problem had a typo in an earlier version of this document,

    Using a for a type variable, the type is f: ([a],a) -> [a].

  12. What is the most general polymorphic type of function g define by g x y = x(head y)?

    Using a and b for a type variables, the type is g: (a -> b) -> [a] -> b

  13. What kind of thing is g x, where g is the function of the preceding problem?

    g x is a function.

  14. What is a strongly typed language? A weakly typed language? An untyped language?

    In a strongly typed language, all type checking is done at compile time. In an untyped language, all type checking is done at run time. In a weakly typed language, some type checking is done at compile time and some at run time.

  15. What is the value of Scheme expression (f '(a b c)) if function f is defined by
        (define (f x)
          (if (null? x) x
              (cons (car x) (cons (car x) (f (cdr x))))
          )
        )
     

    (a a b b c c)

  16. What is does the dispatcher do in the implementation of an object-oriented programming language?

    It examines tags of objects and finds functions for those objects, from the tags and the names of the functions.

  17. What is a virtual function?

    A virtual function is a function that logically belongs at a level in the class hierarchy above the level where it can be defined.

  18. In Java, what is the major difference between a protected variable and a private variable?

    A private variable is available only in one class. A protected variable is available in a class and all of its subclasses.

  19. How are variables inherited in single-inheritance object-oriented languages? What is the mechanism?

    They are inherited by position. They are always in the same place in the representation of an object, regardless of the actuall class of the object.

  20. How is a Java interface like a class? How does it differ?

    An interface is like an abstract class that only contains virtual functions. (It has no data or nonvirtual functions.) A class can implement several interfaces but can only extend one class.

  21. What does the word static mean in Java? What does the word final mean?

    It means the the variable or method is associated with the class itself, not with the objects that belong to the class.

  22. In the implementation of programming languages, how does a function find its nonlocal variables?

    It looks at the static link in its activation.

  23. What is a function closure?

    A function closure is a combination of a function (given by its executable code) and an environment. The environment is the same thing as a static link in an activation.

  24. If you unify g(f(X,a),Y) with g(Z,h(W)), what is the result of the unification? Does it succeed? Here, upper case letters are variables and lower case letters are constants.

    It succeeds. The result is g(f(X,a),h(W)), with bindings Z = f(X,a) and Y = h(W) being done.

  25. Consider the following BNF grammar.
        <S>  ->  a<S> a
        <S>  ->  b<S> b
        <S>  ->  c
     
    Show that string abbcbba can be derived from <S> by showing a parse tree for it.

                      S
                     /|\
                    / | \
                   a  S  a
                     /|\
                    / | \
                   b  S  b 
                     /|\
                    / | \
                   b  S  b
                      |
                      |
                      c                
     

  26. What does a cut do in a backtracking program?

    It removes control frames from the control stack back to a marker frame. The effect is to prune branches from the backtrack search tree.

  27. Function twist is defined by the following two equations.
        twist([]) = []
        twist(a::b::c) = b::a::twist(c)
     
    (Twist is not defined for every list.) Show an evaluation by substitution of expression twist [2,3,4,5,6,7].

      twist [2,3,4,5,6,7] = 3::2::twist[4,5,6,7]
                          = 3::2::5::4::twist[6,7]
                          = 3::2::5::4::7::6::twist[]
                          = 3::2::5::4::7::6::[]
                          = [3,2,5,4,7,6]
     

  28. Write an equational definition of a function called lefts that takes a list of ordered pairs and produces a list of the left-hand members of those ordered pairs. For example, lefts([(2,4), (3,5), (7,6)]) = [2,3,7]. Do not use the map function.

       lefts ([])   = []
       lefts (h::t) = left(h) :: lefts(t)
     

  29. The append predicate is defined as follows in Prolog.
           append([], X, X).
           append([H|T], X, [H|Y]) :- append(T, X, Y).
     
    (Recall that [H|T] is the list whose head is H and whose tail is T, and that :- is the reverse implication, <=.) Show a proof tree, with variable bindings on the branches, for goal append(X, [3,4], [1,2,3,4]). Show all branches of the computation, including failed branches. Be sure to copy variables where copying is required.

    I will skip writing bindings that have no use in the end.

                      append(X, [3,4], [1,2,3,4])
                              |
                              | X = [H1|T1], H1 = 1
                              |
                      append(T1, [3,4], [2,3,4])
                              |
                              | T1 = [H2|T2], H2 = 2
                              |
                      append(T2, [3,4], [3,4])
                              |
                              | T2 = []
                              |
                           success
     
    The result is X = [H1 | [H2 | []]]. If this is written using the :: operator instead of the bar notation, it is H1::H2::[], or [H1, H2]. So X = [1,2].