CS140 Midterm Exam - October 10, 2006 - Answers
Jim Plank
Answer to Question 1 (10 points)
You may see an explanation of this, plus pictures in the
lecture notes on Queues:
Jval queue_dequeue(Queue q)
{
Jval retval;
True_Queue *tq;
Queue_Node *qn;
tq = (True_Queue *) q;
if (tq->size == 0) {
fprintf(stderr, "Error: Queue_dequeue called on an empty queue\n");
exit(1);
}
qn = tq->front;
tq->front = qn->front;
retval = qn->val;
free(qn);
tq->size--;
return retval;
}
Grading for Question 1 (10 points)
- Casting q to a (True_Queue *): 1 point
- Error-checking on the size: 1 point
- Setting a temporary variable to tq->front: 1 point
- Setting a temporary variable to return: 1 point
- Detaching the front node: 2 points
- Freeing the node: 1 point
- Decrementing the size: 1 point
- Returning the Jval: 1 point
- Getting it all in the correct order: 1 point
Answer to Question 2
When we start off, s is 0x500120, s2 is 0x500130, and
y is 0xbfffed28. This means that the address of x
is 0xbfffed28. The value of x is the address of the
first 'e' in the string s. Since s is equal
to "Juice-em!", that means that x will equal s+4:
0x500124. The statement x[1] = '*' will set the character
after the 'e' to an asterisk. Thus, s now equals
"Juice*em!. So, we can now go over the first eight lines
of the program, after the first one:
0x500124
0xbfffed28
0xbfffed28
0x500124
u
e
0x500121
Juice*em! Juice-em! e*em!
Now, setting *y to s2+2 means that *y is
equal to 0x500132. Since y = &x, this means that x
is also equal to 0x500132. This is the location of the 'i'
in "Juice-em!. So, setting *x to 'X' sets
s2 to "JuXce-em!. Thus, the last two lines of the
program are:
0x500132
Juice*em! JuXce-em! Xce-em!
Grading for Question 2 (10 points)
The program has 11 lines of output. The first is given
for you, and you were to give the remaining ten. Here's
how those remaining ten lines were graded:
- Lines 3 and 5: Half a point each.
- Lines 8 and 10: 1.5 points each: 0.5 per word. If you got
line 8 wrong, but the first word of line 10 was the same as the
first word in line 8, you received the 0.5 for that word.
- All other lines were 1 point. If you missed line 3, but lines
2 and 3 equalled each other, you received a point for line 2.
Ditto lines 1 and 4.
Answer to Question 3 (10 points)
Correctness:
- A. Incorrect, because it ignores zeros.
- B. Incorrect, because it reads from standard input instead
of the command line.
- C. Correct.
- D. This one looks pretty good, but it's not correct,
because it tries to read into d instead of &d.
It will likely segfault.
- E. Correct.
- F. Incorrect for a few reasons. First, it calls atoi,
and then doesn't print out zeros, which will ignore non-numbers,
but it will also ignore zeros. Second, it calls atoi and
not atof, so all decimals will be zero.
- G. Incorrect, because d is an integer, so the
sscanf() will not properly convert the command line
arguments correctly. It will exclude the non-numbers, but
the values that it prints out are undetermined. Since the sscanf()
will modify 8 bytes starting at &d, the four bytes after d
will get corrupted, which could result in undeterministic behavior.
- H. Incorrect, because it reads from standard input, and not the
command line.
Output:
A.
-43.550
66.440 |
B.
No output.
It hangs. |
C.
-43.550
0.000
66.440 |
D.
Undetermined,
most likely
a segfault.. |
E.
-43.550
0.000
66.440 |
F.
-43.000
66.000 |
G.
Undetermined,
most likely:
0.000
0.000
0.000 |
H.
No output.
It hangs. |
Grading for Question 3 (10 points)
0.5 points per program for stating correctness correctly. 0.75 points
per program for stating the output correctly.
Answer to Question 4 (8 points)
The variable j counts the words. When j mod three equals 0 or 2,
the word is enqueued on the queue. When j mod three equals 1 or 2, then
the length of the word is pushed on the stack. The variable ne is the
sum of the number of elements on the stack and queue.
So, the output is:
3 5 5 7 4 1 5 6 2 3 4 3
Cutie bomb Met at a salon With baby louis Under her arm
24
Grading for Question 4 (8 points)
Three points for line 1, three for line 2, two for line 3.
Answer to Question 5
A simple program:
main()
{
int c, lastc;
lastc = -1;
for (c = getchar(); c != EOF; c = getchar()) {
if (c != lastc) putchar(c);
lastc = c;
}
}
You could have used the fields library, but it's a little
more of a pain:
main()
{
int c, lastc, i;
IS is;
lastc = -1;
is = new_inputstruct(NULL);
while (get_line(is) >= 0) {
for (i = 0; is->text1[i] != '\0'; i++) {
c = is->text1[i];
if (c != lastc) putchar(c);
lastc = c;
}
}
}
Grading for Question 5 (8 points)
- Correctly reading from standard input: 2 points
- Omitting characters properly: 2 points
- Printing characters properly: 2 points
- No extraneous mess: 2 points