This page does not represent the most current semester of this course; it is present merely as an archive.
Question 1
nop
Question 2
Questions 3–7
extraneous %, 0x, etc OK.
Correct answers:
> It became clear that we either failed to teach the error detection topics adequately or there was some unnoticed ambiguity in the questions about it; as a consequence, we added 0.75 to every score on each of the questions on this topic to help correct our failure, whichever one it was.
Question 8
D
C
Question 9
none
T
or L
Question 10
T
Question 11
P
Question 12
L
T
Question 13
7
Question 14
Question 15
Question 16
The bug is the malloc
is too small; it should have a sizeof(int)
term. This creates a second bug: we could go off the end of the array (heap buffer overflow) both in the copying loop and potentially in every other access to s.data[index]
.
The leak is fixed by adding a free(s.data)
after the for
and before the s.data = tmp;
.
Graded in four parts:
s.data[index]
(there are three of them)s.data[index]
or the memory leakfree(s.data)
free(tmp)
– note there is no non-buggy way to do this, so they miss the next points toos.data = tmp
Question 17
The set of correct solutions includes
do {
y += 1;
if (x&1) {
x *= 3;
x += 1;
} else {
x >>= 1;
}
} while (x > 1);
There are other solutions; for example,
y += 1; x = (x%2==0) ? x/2 : x*3+1;
while (x>1) { y += 1; x = (x%2==0) ? x/2 : x*3+1; }
… as well as things like for(;;)
with an internal break
, etc.
Don’t be picky on ;
, etc., unless expressly called out below.
Graded in three parts:
goto
or labels;
like(...);{
or do{ ... }; while
if
/else
(or two complimentary if
, etc)*3+1
and the even has the >>1
(or equivalent)