Computer Science 3675
Fall 2016
Practice Questions for Quiz 6

  1. True or false?

    1. When a variable occurs in a logic programming goal, the interpreter is being asked whether that goal holds for all values of the variable.

      Answer

    2. When a variable occurs in a logic programming axiom, the interpreter is being told that axiom holds for all values of the variable.

      Answer

    3. In logic programming, a variable in an axiom might be used as an input variable sometimes and as an output variable at other times, when computation uses that axiom.

      Answer

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

      Answer

    5. A token can only have one associated lexeme.

      Answer

    6. A variable name is typically a single lexeme in a program.

      Answer

    7. Most programming languages have a comment token in their grammars.

      Answer

    8. Most programming languages require two consecutive lexemes to be separated by a space or spaces.

      Answer

  2. Show the logic programming search tree for goal (member(X,[3,4,5]), member(X,[4])), up to the point where a success is found. The definition of the member predicate is as follows, written in Prolog syntax.

          member(M, [M|X]).
          member(M, [X|T]) :- member(M, T).
    

    Answer

  3. In a logic programming style, write axioms for computing predicate prefix(X,Y), which is true just when list X is a prefix of list Y. For example, prefix([2,4,6], [2,4,6,8,10]) is true. Every list is a prefix of itself.

    Answer

  4. In a logic programming style, write axioms for computing predicate allsame(X), which is true just when all members of list X are the same. For example, allsame([5,5,5]) is true, as is allsame([a,a]), but allsame([2,4,4]) is false. Note that allsame([]) is true, and allsame([b]) is true.

    Answer

  5. In a logic programming style, write axioms for computing predicate samelength(X,Y), which is true just when X and Y are lists that have the same length.

    Answer

  6. The append predicate is defined as follows in Prolog.

      append([], Y, Y).
      append([A|X], Y, [A|Z]) :- append(X, Y, Z).
    
    You would like to define a predicate double(X), which is true if list X has the form Y ++ Y, for some Y. For example, double([a,b,c,a,b,c]) is true, but double([a,b,c,a]) is false.

    Which of the following is a correct definition of double in Prolog?

    1. double(X) :- append(X,X,Y).
    2. double(X) :- append(Y,Y,X).
    3. double(X) :- append(X,Y,X).
    4. double(X) :- append(Y,X,X).

    Answer

  7. Using the correct definition of predicate double from the preceding exercise, can a Prolog interpreter handle goal double(Z), where Z is an unbound variable? If so, what will it do? If not, why not?

    Answer

  8. You would like to write a definition of predicate member, where member(X, L) is true if X is a member of list L. Which of the following is a correct definition of member?

    1. member(X, L) :- append(A, [X|B], L).
    2. member(X, L) :- append(X, Y, L).
    3. member(X, L) :- append(L, [X|Z], Y).
    4. member(X, L) :- append([X|L], Y, L).

    Answer

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

    Answer

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

    Answer

  11. 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

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

    Answer

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

    Answer

  14. What is an abstract class?

    Answer

  15. What is Backus-Naur Form, and what is it used for?

    Answer

  16. Show a parse tree for string aacacab according to the following grammar, where the start nonterminal is S. In this grammar, upper case letters are nonterminals and lower case letters are tokens.

        S -> F a S
          |  b
    
        F -> a F
          |  c
    

    Answer

  17. Show that the following grammar is ambiguous. The start symbol is S. Upper case letters are nonterminals and lower case letters are tokens.

        S -> S a
          |  a S
          |  a
    

    Answer