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)


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:

Answer to Question 3 (10 points)

Correctness: 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)