
CS 403/503 Exam 4 Fall 2009 Name __________________
CS 403 students: Your grade is based on your best 8 answers to these 12 problems.
CS 503 students: Your grade is based on your best 10 answers to these 12 problems.
1. Trace this (Squeak) Smalltalk code and show the output in the space indicated.
A := Array new: 6.
1 to: A size do: [:j | x := (((x \\ 2) = 1) “ \\ is the mod operator”
ifTrue: [3 * x + 1]
ifFalse: [x / 2]).
A at: j put: x].
A do: [:y | Transcript show: y; space].
Transcript cr; show: (A collect: [:z | z + 11]); cr.
Transcript show: (A select: [:z | z > 30]); cr.
Transcript show: (A reject: [:z | z > 30]); cr.
Transcript show: (A inject: 0 into: [:y :z | y>z ifTrue: [y] ifFalse: [z]]); tab.
Transcript show: (A inject: 99 into: [:y :z | y<z ifTrue: [y] ifFalse: [z]]); cr.
Output:
2. Trace this (Gnu) Smalltalk code and show the output in the space indicated.
Object class extend [ p: x [ Transcript show: (x printString , self printString); cr] ]
Object subclass: A [
m: x [x < 5 ifTrue: [self n: 5 * x] ifFalse: [self class superclass p: x]]
n: x [x < 5 ifTrue: [self m: 20 * x] ifFalse: [self class p: x]]
]
A subclass: B [ m: x [super m: x+1] ]
B subclass: C [ n: x [super n: x+1] ]
C subclass: D [ m: x [super m: x+1] ]
D subclass: E [ n: x [super n: x+1] ]
X := Array new: 5.
X at: 1 put: A;
at: 2 put: B;
at: 3 put: C;
at: 4 put: D;
at: 5 put: E.
X do: [:y | y new m: 1; n: 1].
Output: