View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Spring 2005 Sample Midterm

Here's the midterm that I gave during the Summer 2004 semester.


Feel free to discuss the questions and possible answers below.


1d. Not 100% sure on this, but I think it deals with how Squeak variables can be bound to an type of variable, e.g.:

foo := 5.
foo := 'bob'.

This is more powerful than Java's implementation, since it allows a great deal of flexibility in the writing of methods. For example:

add: num1 to: num2
^ num1 + num2

This is a very simple example, but it shows how you can give the function several types for "num1" and "num2" and still get valid results (e.g. add: 5 to: 9 OR add: 4.4 to: 9.113).

With Java, you would have to write function for each of the types that could be passed in.
> You would only have to write a function for each of the types that could be passed in if you were passing in primitives rather than derivatives of Number. -Jeff Y

One drawback of this style is that the method can be passed ANYTHING, even invalid input, and you wouldn't know until it crashed during runtime.

Is this what you were looking for in the answer?

3a. class
b. instance
c. instance

1 to: 4 do: [:i | Transcript show: ((5 + (i 2 - 4)) negated + 3) printString , ' is next'; cr].


#(1 2 3 4 5)

#(1 2 3 4 5) - someone explain this.

What happens here is that the item is getting doubled, but is not getting set to anything. Since this is a do loop, it will just execute the statement and nothing else. But as in the one below, collect will create a new collection of elements. So essentially, this is the same as a := b.

A further note on b := do: [ :item | item x 2 ]. The message do: iterates over the elements of the collection and hence item is a reference to SmallInteger in this example. Even if the block statement was item := item x 2, item is a local scope variable to the block. Think objects! The statement item := item x 2 would effectively double the current item, but remember since SmallIntegers are objects, item x 2 returns a new instance of SmallInteger and the original item reference from the array is lost (and is further left untouched). In addition, do: returns the original Collection upon completion. Therefore, this loop has no effect other than a := b. Matt B

#(2 4 6 8 10)

courtesy -Tony