Sample Midterm

These are sample questions that are very similar to the ones I will ask on the midterm.

  1. Which of the following is not a valid C variable name?
    1. hello
    2. chomp_burp
    3. whilex
    4. more-more-more
    5. TrUcKiNg

  2. Evaluate the following expressions, assuming a = 1, b = -3, and c = 0. Treat them independently, so (for example) after evaluating (b), the above values are used for (c).
    1. a + b < c
    2. c == !a == c
    3. a = ++b; give the values of both a and b as well
    4. a– == !b; give the value of a as well
    5. a / b

  3. True or False: If x = -1, then if (x) printf("1"); else printf("2"); prints 2.

  4. What are all possible outputs of the following code fragment?
    void f(int a, int b)
    {
        printf("%d %d\n", a, b);
    }

    void main(void)
    {
        int i = 5;
        f(++i, ++i);
    }

  5. Given the definitions
        int numbs[10];
        int *ptr = numbs;
    which of the following are equivalent, and why?
    1. numbs[3]
    2. numbs + 3
    3. *(numbs + 3)
    4. *(ptr + 3)
    5. *ptr + 3

  6. 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?<br>
    2. Is the following while loop equivalent? If not, how does its result differ? (: look at the values of both x and i.)
      x = i = 0;
      while( i++ <= 100)
          x += ++i;
    3. Does the following for loop do the same thing? If not, what does it do?
      for(x = i = 0; i <= 100; i++){
          if (!(i % 2))
              continue;
          x = x + i;
      }

  7. What does this function do?
    char *x(char *s, char c)
    {
        char *r = NULL;

        do{
        while(*s && *s != c)
            s++;
        if (*s)
            r = s;
        } while(*s++);
        return(r);
    }

  8. 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)
    {
        ++acount;
    }

    int main(void)
    {
        int c;
        int counter = 0;

        while((c = getchar()) != EOF)
            if (c == ’a’ || c == ’A’)
                a_again(counter);

        printf("%d\n", counter);
        return(0);
    }


You can also obtain a PDF version of this. Version of October 21, 2015 at 5:20PM