Computer Science 3675
Fall 2017
Practice Questions for Quiz 4

  1. What is one important motivation for including exception handling in a programming language?

    Answer

  2. How is exception handling implemented in a typical language, such as Java?

    Answer

  3. Are backtracking and exception handling the same thing? For example, can you use the exception handling mechanism of Java to do backtracking? If they are different, what is the difference in their mechanism?

    Answer

  4. Using backtracking, write a Cinnameg program fragment that will print all solutions (x,y) to equation xy − 2x2 + y = 10, where x and y are both integers in the range 0,...,100. Do not use a loop or recursion. The program should have a straight-line look to it. That is, it has the look of a sequence of a fixed number of steps.

    Answer

  5. In Cinnameg, each(ℓ) uses backtracking to create a branch for each member of list ℓ. For example, each([4,6,8]) creates three branches. In the first branch, it yields 4. In the second branch, it yields 6. And in the third branch, it yields 8.

    How many lines does the following Cinnameg program fragment write?

        Try
          !x = each [1, 2, 3].
          !y = each [10, 11, 12, 13].
          !z = each [100, 101].
          Displayln (x, y, z).
          Ensure false.
        %Try
    

    Answer

  6. What does unification do?

    Answer

  7. Unification is a form of pattern matching. Which of the following is not a characteristic of unification?

    1. Unification never changes the binding of a bound variable.
    2. Unification is symmetric; unifying A with B has exactly the same effect as unifying B with A.
    3. Unification is very slow, and is only used rarely during computations of logic programs.
    4. Unification can bind unbound variables.

    Answer

  8. Give a collections of substitutions that unifies terms f(g(4,X),Y) and f(g(X,W),Z), where X, Y, Z and W are variables. Choose substitutions that do not many unnecessary restrictions.

    Answer

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

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

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

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

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

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

  15. From fall16/practice/practice06.html:
  16. 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

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