Princeton University COS 217: Introduction To Programming Systems Spring 2018 Midterm Exam Answers
Princeton University COS 217: Introduction To Programming Systems Spring 2018 Midterm Exam Answers
Princeton University COS 217: Introduction To Programming Systems Spring 2018 Midterm Exam Answers
Question 1 (a)
0 0 1
Question 1 (b)
3 + 6 = 1 Unsigned overflow occurrred.
Question 1 (c)
3 + -2 = 1 Signed overflow did not occurr.
Question 1 (d)
4
Question 2 (a)
5 1 2
Explanation:
(i << 2) | (j >> 1)
(1 << 2) | (j >> 1)
00000000000000000000000000000001(binary) << 2) | (j >> 1)
00000000000000000000000000000100(binary) | (j >> 1)
00000000000000000000000000000100(binary) | (2 >> 1)
00000000000000000000000000000100(binary) | 00000000000000000000000000000010(binary) >> 1)
00000000000000000000000000000100(binary) | 00000000000000000000000000000001(binary)
00000000000000000000000000000101(binary)
5
Note that neither the << nor the >> operator changes the value of its left operand. So the
values of i and j are unchanged.
Question 2 (b)
4 2 3
Explanation:
i++ + ++j
1 + ++j (The value of i is not changed at this point)
1 + 3 (Side effect: value of j becomes 3 at this point)
4
(Side effect: value of i becomes 2 at this point)
Question 2 (c)
error
Page 1 of 5
Explanation:
The operand of the –- operator must be a l-value. That is, the operator of the –- operator must
be such that it could appear as the left operand of a = operator.
Question 2 (d)
-3 -3 4
Explanation:
i -= (j += 2)
i -= 4 (side effect: value of j becomes 4)
-3 (side effect: value of i becomes -3)
Question 2 (e)
error
Explanation:
The left operand of the = operator must be a l-value. 1 + i is not a l-value.
Question 2 (f)
1 1 2
Explanation:
Question 3
Asserts at the beginning of SortedList_insert():
assert(list != NULL);
assert(SortedList_isSorted(list));
assert(SortedList_isSorted(list));
assert(SortedList_isAtPosition(list, pos, newval));
Page 2 of 5
/* Return 1 (TRUE) if list is sorted, and 0 (FALSE) otherwise. */
prevval = currnode->value;
currnode = currnode->next;
while (currnode != NULL)
{
if (currnode->value < prevval)
return 0;
prevval = currnode->value;
currnode = currnode->next;
}
return 1;
}
prevnode = currnode;
currnode = currnode->next;
while (currnode != NULL)
{
if (currnode->value < prevnode->value)
return 0;
prevnode = currnode;
currnode = currnode->next;
}
return 1;
}
/* Return 1 (TRUE) if newval is at position pos within list, and 0 (FALSE) otherwise.
static int SortedList_isAtPosition(const struct SortedList *list, int pos, int newval)
{
Page 3 of 5
const struct Node *currnode;
int currpos = 0;
for (currnode = list->first; currnode != NULL; currnode = currnode->next) {
if (currpos == pos)
return (currnode->value == newval);
currpos++;
}
return 0;
}
/* Return 1 (TRUE) if newval is at position pos within list, and 0 (FALSE) otherwise. */
static int SortedList_isAtPosition(const stuct SortedList *list, int pos, int newval)
{
int currpos = 0;
const struct Node *currnode = list->first;
while ((currnode != NULL) && (currpos < pos))
{
currnode = currnode->next;
currpos++;
}
if (currnode == NULL) return 0;
if (currnode->value != newval) return 0;
return 1;
}
Question 4
In the following explanations assume that the value of pi1 is (pretend) address 1000. That is, assume
that pi1 points to an 8-byte chunk in the HEAP section of memory that begins at (pretend) address 1000.
Then the value of pi2 is (pretend) address 1004. Furthermore, assume that the 1 which is the value of
i resides in the STACK section of memory at (pretend) address 2000.
Question 4 (a)
BD
Explanation:
• free(pi1) frees the 8-byte HEAP chunk that begins at address 1000.
• *(pi2-1) = i tries to assign 1 to the first four bytes of the 8-byte HEAP chunk that begins at
address 1000. Since that chunk was freed, B occurs.
• free(pi2) tries to free a chunk that begins at address 1004. But no such chunk was allocated in
the HEAP. So D occurs.
Question 4 (b)
E
Explanation:
• free(pi1) frees the 8-byte HEAP chunk that begins at address 1000.
• pi2 -= 1 assigns 1000 to pi2.
• free(pi2) frees the HEAP chunk that begins at address 1000. But that chunk already was freed.
So E occurs.
Question 4 (c)
F
Explanation:
• *(pi1 + 1) = i assigns 1 to the 4-byte area of the HEAP section that begins at address 1004.
That area is within the 8-byte HEAP chunk that begins at address 1000. So that's fine.
• pi2 += 1 assigns 1008 to pi2. The fact that pi2 points to unallocated memory is not a problem.
That would be a problem if and only if pi2 were dereferenced.
• free(pi1) frees the 8-byte HEAP chunk that begins at address 1000.
Page 4 of 5
Question 4 (d)
ACD
Explanation:
• (*pi2 + 1) = i tries to assign 1 to the 4-byte memory area that begins at address 1008. But
memory at address 1008 was not allocated. So A occurs.
• pi1 += 1 assigns 1004 to pi1.
• free(pi1) tries to free a chunk that begins at address 1004. But no such chunk was allocated in
the HEAP. So D occurs.
• Moreover, the 8-byte HEAP chunk that begins at address 1000 is not freed. So C occurs.
Question 4 (e)
CD
Explanation:
• *pi1 = i assigns 1 to the 4-byte area of the HEAP section that begins at address 1000. That
area is within the 8-byte HEAP chunk that begins at address 1000. So that's fine.
• pi1 = &i assigns address 2000 to pi1.
• free(pi1) tries to free a chunk of memory that begins at address 2000. But no such chunk was
allocated in the HEAP. (That address is in the STACK.) So D occurs.
• Moreover, the 8-byte HEAP chunk that begins at address 1000 is not freed. So C occurs.
Copyright © 2018 by Szymon Rusinkiewicz, Donna Gabai, and Robert M. Dondero, Jr.
Page 5 of 5