The test is open-everything with the sole limitation that you neither solicit nor give help while the exam is in progress.
Submit all answers on these exam sheets. No extra sheets are allowed. If you are nervous about this, work out the problem on a separate sheet and copy your answers here. Work quickly but carefully. I don't suspect many people will finish all the problems; the intent is to make sure the people most familiar with the material get the best grades.
| Problem | You got | Out of |
|---|---|---|
| 1 | 20 | |
| 2 | 20 | |
| 3 | 20 | |
| 4 | 20 | |
| 5 | 20 | |
| TOTAL | 100 |
a = Account.new(14)
a.id returns 14
a.balance returns 0
a.balance = 100 prints "depsited 100 to account 14", returns 100
a.balance = 20 prints "withdrew 80 from account 14", returns 20
a.balance = -20 prints "overdrafts not allowed", returns -20
a.balance returns 20
var a = [];
for (var i = 0; i < 10; i++) {
a[i] = function() {return i;}
}
var b = [];
for (var j = 0; j < 10; j++) {
b[j] = a[j]();
}
At the end of this script, what is the value of b? Explain in detail why this is. A sketch will be very helpful and improve your chances of getting full credit. Running the script and simply reporting the value of b is worth only 2 points out of 20, so make sure you can explain the value.
For example, if you pass as arguments the function that doubles its inputs, and the list [4, 3, 1, 2, 2], then the return value would be [4, 6, 4, 16, 32].
Hints: Do the f(f(f...)) as a separate function. Also you do NOT have to make your function tail recursive.