Docsity

CS 403/503 Exam 4 Fall 2009 - Smalltalk Coding Questions, Exams of Programming Languages

Toggle sidebar
bg1
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.
x := 11.
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:
pf3

Partial preview of the text

Download CS 403/503 Exam 4 Fall 2009 - Smalltalk Coding Questions and more Exams Programming Languages in PDF only on Docsity!

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. x := 11. 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:

For the remaining problems, write each of these methods using Smalltalk. Except for method printOn:, each method definition is not permitted to call any other method with the identical name from any other class.

  1. Write method printOn: for the ComplexNumber class. Assume that ComplexNumber has two instance variables, real and imag. Example outputs: 5-2i, -7+6i.
  2. Write method printOn: for the Array class. Example outputs: #(10 20 30), #().
  3. Write method do: for the Array class.
  4. Write method collect: for the Array class.
  5. Write method asSet for the Array class.