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

Spring2004 Midterm Review: Reading Code

1. abcdefghijkl
when you call do: on an object, you follow it with a block that is set up like in the example, each can be anything and is the 'variable' for each item in the array

2. 4

3. infinite, [test] might/should be [i < 10], in which case the answer would be 9

that's right, you'd need the following for it to work:
| i test |
i := 1.
test := [i < 10].
test whileTrue: [Transcript show: 'hello'.
	i := i + 1.].

I didn't actually write this review problem, so I'm not sure which way it was meant. In any case, the above answer is correct for the two possibilities. Rich LeBlanc

3) Be careful. I've DoneIt in Squeak, and it will go forever, since the value of test does not change with the value of i.

1) If you don't omit the comma it will still work just not as would be intuitive. Squeak will interpret the comma as a character literal so that you get an array that looks like this:
  #('abc' 'def' 'ghi' #, 'jkl')
2) In that case the do: would loop 5 times and the output would be
You could use the comma, you just couldn't use the literal representation of the array. You would have to do soemthing like:
 anArray := Array with: 'abc' with: 'def' with: 'ghi', 'jkl'
and get
 #('abc' 'def' 'ghijkl')
3) Like the guy above said above, as written it would be infinite. Probably the code was intended to be something like:
test := [i < 10].
test whileTrue: [Transcript show: 'hello'.
    i := i + 1].
That way test is a block that is executed each loop and will print the correct number of times.