Problem 1
a)
First cmethod(b) + a + b, now a=10, b=2
Next compute cmethod(2) which is bmethod(1) + a + b, now a=40 and b=20
Next compute bmethod(1) which is a + b + dmethod(1) now a=10 and b=30
Next compute dmethod(1) which is a + cmethod(1) now a=1 and b=30
Next compute cmethod(1) which is bmethod(0) + a + b now a=40 and b=20
Next compute bmethod(0) which is a + b + dmethod(0) now a=10 and b=30
Next compute dmethod(0) which is a + cmethod(0) now a=0 and b=30
Next compute cmethod(0) which is a + b now a=40 and b=20
Thus, cmethod(0) = 40 + 20 = 60
dmethod(0) = 0 + 60 = 60
bmethod(0) = 10 + 30 + 60 = 100
cmethod(1) = 100 + 40 + 20 = 160
dmethod(1) = 1 + 160 = 161
bmethod(1) = 10 + 30 + 161 = 201
cmethod(2) = 201 + 40 + 20 = 261
cmethod(b) + a + b = 261 + 10 + 2 = 273
b)
First cmethod(b) + a + b, now a=10, b=2
Next compute cmethod(2) which is bmethod(1) + a + b, now a=40 and b=2
Next compute bmethod(1) which is a + b + dmethod(1) now a=40 and b=30
Next compute dmethod(1) which is a + cmethod(1) now a=1 and b=30
Next compute cmethod(1) which is bmethod(0) + a + b now a=40 and b=30
Next compute bmethod(0) which is a + b + dmethod(0) now a=40 and b=30
Next compute dmethod(0) which is a + cmethod(0) now a=0 and b=30
Next compute cmethod(0) which is a + b now a=40 and b=30
Thus, cmethod(0) = 40 + 30 = 70
dmethod(0) = 0 + 70 = 70
bmethod(0) = 40 + 30 + 70 = 140
cmethod(1) = 140 + 40 + 30 = 210
dmethod(1) = 1 + 210 = 211
bmethod(1) = 40 + 30 + 211 = 281
cmethod(2) = 281 + 40 + 2 = 323
cmethod(b) + a + b = 323 + 10 + 2 = 335
Problem 2
Java use strict name equivalence when determining if two primitive types are compatible. For example type char and type double are not compatible.
Java use loose name equivalence for non-primitive types. For example, subclass C is compatible with its parent class P. And class Z is not compatible with class P.
class P {
}
class Z{
}
class C extends P {
}
Problem 3
a)
list[] = {1, 2, 3, 4, 5}
Because in call-by-value, only copies of list elements change, list elments themself keep the same.
b)
list[] = {1, 3, 4, 4, 5} Because in call-by-reference, the referenced list element changes.
Problem 4
Reference counters and mark-and-sweep are equally good. Both have their strength and weakness. The strength of reference counters is the tombstones can be immediately freed. The weakness is that it must record the reference count and update it each time number of references changes. The weakenss of mark-and-sweep is that it can take time to finish a run. The strength of mark-and-sweep is that there is no need recording reference count using mark-and-sweep.
Problem 5
a)
For a function of type a -> b, it can be seen as a implies b. Suppose function f1 with type a->b, and function f2 with type b->c, then the composition f(x) = f2 (f1(x)) has type a->c. f1 can be seen as a implies b, f2 can be seen as b implies c, and the composition f can be seen as a implies c. Thu, the rule for type of function composition is the same with hypothetical syllogism.
b)
By currying, to prove h has type String → String → double, we can first prove h(s1) has type String → double, then with implication introduction to have h has type String → String → double. Also by currying, f(g(s1)) has type int → double, the composition of it with g (String -> int) has type String → double. Thus h(s1) has type String → double and has type String → String → double.