CSC 1800 Midterm Exam
David Matuszek, Fall 2000
Villanova University
Name_____________________________________

Part I -- Prolog

  1. (2 points) In Prolog, what is special about the variable whose name consists of a single underscore?



  2. (2 points) In Prolog, how are variables distinguished from literal atoms?



  3. (12 points) For each of the following, tell whether the unification succeeds, and if it succeeds, what additional unifications occur.

    1. loves(John, Mary) = loves(Mary, Bill)


    2. 2 + 3 = 3 + 2


    3. brother(john, X) = sister(Y, mary)


    4. mother(john, spouse(X)) = mother(X, Y)


    5. \+ (father(X, Y) = father(john, bill))


    6. [X | Y] = [1, 2, 3]


  4. (10 points) There is an error in each of the following statements. Briefly indicate, for each statement, what the error is. (If there is more than one error, mention only one.)

    1. describe(meadow) :- write("You are in a beautiful sunlit meadow.").


    2. mortal(X) :- man (X).


    3. take(X) :- heavy(X), write('You can't lift that!'), !, fail.


    4. Happy(X) :- healthy(X), wealthy(X), wise(X).


    5. error(X) :- write('Error: '), write(X). nl.


  5. (9 points) Assume that the Prolog database contains at least the following information (and possibly more clauses like these):
         male(tom).
         male(dick).
         female(mary).
         female(jane).
         mother(mary, tom). /* Mary is the mother of John */
         mother(mary, dick).
         mother(mary, jane).
    Further assume that people with the same mother also have the same father, that is, you don't have to worry about half-sisters or half-brothers.

    1. Write a predicate daughter(X,Y) to decide whether X is the daughter of Y.




    2. Write a predicate sibling(X,Y) to decide whether X and Y are siblings (have the same parents). Be careful to ensure than no one is his/her own sibling. (Hint: use two clauses.)





    3. Write a predicate brother(X,Y) to decide whether X and Y are brothers. You can use sibling(X,Y) if you like.




Part II -- Smalltalk

  1. (8 points) What is the order of precedence in Smalltalk? In other words, in what order are the operations performed? (There are five rules; I'll supply the first one.)

      1. Anything within parentheses is done first.


      2.  


      3.  


      4.  


      5.  


  2. (4 points) In Smalltalk, a name begins with a capital letter if it is the name of ______________ or _______________ .

  3. (2 points) If you send a message to a Smalltalk object, that object is called the _______________ .

  4. (10 points) What Smalltalk message would you use:

    1. to create an instance of a class?

    2. to convert an integer to a string?

    3. to send a string to another window (for example, the Transcript window)?

    4. to open a file for reading?

    5. to loop until a condition became false?

     

  5. (4 points) Write the simplest possible Smalltalk loop to display the elements of an array named myArray in the Transcript window. Do not include spaces or newlines in the output. (Hint: avoid indexing the array.)




  6. (4 points) Suppose you define a Person object in Smalltalk that has a name instance variable.

    1. Define (write the code for) a getter method for name.



    2. Define a setter method for name.


 

Part III -- General

  1. (6 points) Briefly distinguish between the following three terms:
    |
    1. procedure (or subroutine)


    2. function


    3. predicate


  2. (3 points) In object-oriented languages, if we want to be precise, we should say we "send a message to an object." It is technically incorrect to say we "call" a method. Why is this incorrect?





  3. (4 points) Consider the following program in some (imaginary) language:
    program main {
       int n = 5;
       call proc(n);
       printline n;
    }
    procedure proc(x) {
       x = x + 1;
       printline x;
    }
    1. What lines will be printed if the parameter is passed by value?


    2. What lines will be printed if the parameter is passed by reference?

  4. (4 points) Some languages allow you to declare integer subranges, for example  int n subrange 1..10 declares an integer named n with allowable values 1 through 10. Briefly name two possible reasons for having such a construct in the language.

    1.  


    2.  


  5. (3 points) Your instructor claims that the && and || operators in Java are actually control structures. Why?




  6. (4 points) Assume that <letter> and <digit> are defined in the usual way. The following BNF defines a <name>. Give a precise English language definition of <name>. Do not use examples.
    <alphanum> ::= <letter> | <digit>
    <name> ::= <letter> | <letter> <alphanum> | <letter> _ <alphanum>








  7. (6 points) The Bohm-Jacopini Theorem states that three control structures are sufficient to construct any program. What are those three control structures?

    1.  

    2.  

    3.  



  8. (3 points) What does it mean to say that a variable has dynamic scope?