CSE 341 -- Smalltalk Quiz

November 15, 1995


50 minutes, open book and notes, 60 points total

Your name:


  1. [3 points per subquestion -- 15 points total] What is printed in the transcript after evaluating the following Smalltalk code? If there would be an error, say so.
    1. 
      | k |
      k := 1.
      (k<10) ifTrue:
        [Transcript show: k printString.  Transcript space.  k := k+k].
      
      

    2. 
      | k |
      k := 1.
      (k<10) whileTrue:
        [Transcript show: k printString.  Transcript space.  k := k+k].
      
      

    3. 
      | k |
      k := 1.
      [k<10] ifTrue:
        [Transcript show: k printString.  Transcript space.  k := k+k].
      
      

    4. 
      | k |
      k := 1.
      [k<10] whileTrue:
        [Transcript show: k printString.  Transcript space.  k := k+k].
      
      

    5. 
      Transcript show: (1+4 sqrt*3) printString.
      
      

  2. [10 points] Compare the mapcar function in Lisp with the collect: message to collections in Smalltalk. How are they similar? How are they different?

  3. [10 points] Consider the evaluation of the following Smalltalk code.
    | a |
    a := Array new: 3.
    a at: 1 put: 3.5.
    a at: 2 put: (a at: 1) + 4.2.
    (a at: 2) <= (a at: 1) ifTrue: [Transcript show: 'smaller'].
    
    Explain in detail what objects are created, what messages are sent to which objects, and how the control structure works.

  4. [10 points] The purpose of this question is to test your understanding of how Smalltalk message lookup is performed. Suppose that we have a class PinballGame that is a subclass of Object, and a class FancyPinballGame that is a subclass of PinballGame.

    PinballGame defines the following methods:

       tiltMessage
         ^ 'TILT!! TILT!! TILT!!'
    
       tilt
         Transcript show: self tiltMessage.
    

    FancyPinballGame defines the following methods:

       tilt
         super tilt.
         SoundGenerator speak: 'call the police!!'.
    
       tiltMessage
         ^ 'This is a highly sophisticated device.  Don't mess with me!"
    

    What happens when you send the message tilt to an instance of PinballGame? To an instance of FancyPinballGame? (Suppose that SoundGenerator is an object that generates spoken speech.)

  5. (5 points) Question for both the cautious and the daredevil! Do metaclasses represent a major simplification and improvement of understandability of object-oriented languages? If you think the answer is no, just say "no" and you will get full credit for this question. If you think the answer is yes, say "yes" ... but for credit you must also correctly answer this question:

    The revised new method for Stack class was defined as follows:

    new ^super new setsize: 3 Explain in detail what happens when Stack new is evaluated.

    Finally you can answer "no" to the first question but still answer the second question. You won't get any extra credit but you will get a colorful dinosaur stamp on your quiz if your answer is correct.

  6. (10 points) Tacky but easy-to-grade true/false questions!

    1. Certain methods, such as floating point addition, are implemented as primitives, which invoke operations in the underlying virtual machine.
    2. For compatibility with existing languages, Smalltalk includes C-like syntax for control structures such as conditionals and loops.
    3. An object's instance variables are always accessible to other objects via automatically defined methods that access and set that instance variable.
    4. A framework includes a useful set of classes, designed to be subclassed to implement a particular application.
    5. Class variables are just like instance variables, but hold references to classes rather than instances.