Sample Final

Here's an example of the sort of questions I have asked in the past.

  1. What does the following function do? Please answer in a single, short sentence.

    char *x(char *s, char c)
         char *r = NULL;
              while(*s && *s != c) s++;
                   if (*s) r = s; 
         } while(*s++);
  2. In C, it is often said that the name of an array and a pointer are the same thing. Let's look at this a bit more closely using the following declarations:
    char a[27] = "abcdefghijklmnopqrstuvwxyz";
    char c;
    Here, the array a begins at memory address 0x1010, the variable b is at address 0x3040, and c is at address 0x4050. Treating each of the following independently (that is, at the beginning of each, the variables are as shown above), and remembering that "illegal assignment" is a valid response, what is ...

    1. the value of b after b = &c;
    2. the value of a after a = &c;
    3. the value of b after b = a;
    4. the value of a after a = b;
    5. the value of c after c = *(a+5);
    6. the value of c after c = 7[a];
    7. the value of c after b = a; c = b[17];

    Now consider function calls. In a program, we have:

    (where a is as above) and later on, we have defined
    void reverse(char s[]){ ... }
    Assume in the following that reverse does in fact reverse the contents of the array s.
    1. Will this correctly cause the contents of a to be reversed?
    2. If reverse had the argument char *s rather than char s[], would it cause the contents of a to be reversed?
  3. What does the following print?
    	struct node {
         		char	letter;
    		struct node *next;
    	} a, b, c, *ptr;
    	a.letter = 'A';
    	b.letter = 'B';
    	c.letter = 'C'; = &b; = &c; = NULL;
    	ptr =;
    	while (ptr != NULL) {
    		printf("%c\n", ptr->letter);
    		ptr = ptr -> next;
  4. The following segment of code is supposed to print the number of times the routine a_again is called. Yet, regardless of the input, it prints 0. Why? How would you fix it?
    void a_again(int acount)
    void main(void)
    	register int c;
    	int counter = 0;
    	while((c = getchar()) != EOF)
    		if (c == 'a' || c == 'A')
    	printf("%d\n", counter);
  5. The dot product of two vectors is simply the sum of the product of each component. For example, the dot product of the vectors (5, 3, 8, 9) and (2, -1, 3, -2) is
    5 x 2 + 3 x (-1) + 8 x 3 + 9 x (-2) = 10 + (-3) + 24 + (-18) = 13

    You are to write the body of the function dot, which is to compute the dot product of the two vectors a and b. The vectors are represented as integer arrays of length n. Below is the declaration.
    int dot(int a[], int b[], int n)
  6. What are all possible outputs of the following code fragment?
    	void f(int a, int b)
    		printf("%d %d\n", a, b);
    		int i = 5;
    		f(++i, ++i);
  7. Write a recursive function to add the integers from a to b. You may assume that a <= b initially.
  8. Use the following code fragment to answer parts a, b, and c:
    		for(x = i = 0; i <= 100; i += 2, x += i);
    1. In one short sentence , what does this for loop do?
    2. Is the following while loop equivalent? If not, how does its result differ?
      		x = i = 0;
      		while( i++ == 100)
      			x += ++i;
    3. Is the following for loop print the same thing? If not, what does it print?
      		for(x = i = 0; i <= 100; i++){
      			if (!(i % 2))
      			x = x + i;
  9. What does the following function do?
    		x(char *s, char *t)
    			for( ; *s == *t; s++, t++)
    				if (*s == '\0')
    			return(*s - *t);
  10. The following code fragment reads a line from the standard input and prints out the first half of the line. The underlying algorithm to determine the midpoint of the line and print out the first half is correct. But this program is horribly non-portable and non-robust. Point out 3 poor programming practises affecting robustness and portability, and say how those 3 hould have been done portably and robustly.
    	char c;
    	char *buf, *p, *q;
    	p = buf = malloc(sizeof(char) * 100);
    	while ((c = getchar()) != EOF && c != '\n')
    		*p++ = tolower(c);
    	*p = '\0';
    	printf("first half of string is: ");
    	for(q = buf; q < (buf + p) / 2; q++)

Send email to

Department of Computer Science
University of California at Davis
Davis, CA 95616-8562

Page last modified on 12/1/97