Midterm Exam Name ______answer key________________

CSC 7000-002 Algorithms & Programming
Fall 1999, Mendel 115
Dr. David Matuszek

  1. Circle the words that are keywords in C:

    if  then  else  while  do  repeat  until  signed

  2. Circle the words that are legal variable names in C:

    top_job  register  _important_   id#5  $limit  Default

  3. For each of the following code fragments, either (1) if it is legal, tell exactly what it will print, or (2) if there is a syntax error, show how to correct it. (Don't do both!) Assume that the code fragment is part of a larger program that declares all variables correctly, etc. Use       to indicate a blank and         to indicate a newline.
    i = 5; j = i++;
    printf ("i=%d, j=%d\n", i, j);
    i=6, j=5
    i = 5; j = --i;
    printf("%d\n", i>j ? i : j);
    4
    i = 0; j = 10;
    while (i < j)
      i++; j--;
    printf ("i=%d, j=%d\n", i, j);
    i=10, j=9
    int a[10];
    for (i=j=0; i<10; i++,j--)
      a[i]=j;
    printf ("a[2]=%d\n", a[2]);
    a[2]=-2
    i = rand() % 10 / 100;
    printf ("i=%d\n", i);
    i=0
    i = 15;
    printf ("%d %o %x\n", i, i, i);
    15 17 f
    char *p;
    for (p = "hello"; *p; ++p)
      printf ("%c", *p + 1);
    ifmmp
    i = 2;
    switch (i) {
      case 3: printf ("three");
      case 2: printf ("two");
      case 1: printf ("one");
      default: printf ("zero");
    }
    twoonezero
    i = 10; j = 3;
    k = i - j - 2 * j;
    printf ("k=%d\n", k);
    k=1
    i = 1; j = 2;
    printf ("%d\n", i > j);
    0
  4.  

  5.   For each of the following, circle T (for TRUE) or F (for FALSE):
     
    1. T  F  In C, every string begins and ends with a zero byte.
       
    2. T  F  Variable names can be any length.
       
    3. T  F  When you pass an array to a function, the entire array is copied.
       
    4. T  F  When you pass a struct to a function, the entire struct is copied.
       
    5. T  F  When you call a function without arguments, you still need the parentheses.
       
    6. T  F  All variable declarations must occur within functions.
       
    7. T  F  The compiler initializes all variables to zero.
       
    8. T  F  The do...while loop controls only a single statement.
       
  6. In just a few words each, identify each of the following operators.
    a & b bitwise AND of a and b
    & b address of b
    a && b logical AND (short circuit) of a and b
    a % b mod, remainder of a/b
    a += b add b to a
    a != b a not equal b
    a >> b right shift a, b bits
    a . b field b of record a
    a -> b field b of record *a
     

  7.  The following function takes as arguments the name of a file, and a string, and writes the string to the file. Complete the function by filling in the blanks.
    void write_message (char *filename, char *message) {
       /* Declare a FILE variable to use in this function */
           FILE *f;
    
       /* Open the file for writing */
           f = fopen (filename, "w");
    
       /* Write the message onto the file */
           fprintf (f, "%s\n", message);
    
       /* Close the file and return */
           fclose (f);
    
    }
    
  8. Our function main actually has arguments, but most of our programs so far have ignored those arguments. Write a prototype for main that shows what the arguments actually are.
        int main (int argc, char *argv[]);


  9.  Write a statement that prints out the two-character sequence \n, followed by a newline. That is, it should print the three characters \, n, and newline.
        printf ("\\n\n");
    
  10.  Write a loop that sets the array a[100] to all zeros.
        for (i = 0; i < 100; i++) a[i] = 0;


  11.  (a) Declare a struct person to hold a person's name, age, and phone number.
        struct person {
            char *name;
            int age;
            long int phone;
         };


    (b) Now declare two variables of the type you have just defined.

         struct person jack, jill;


    (c) Write code to fill in all the fields of one of your variables with reasonable values.

         jack.name = (char *) malloc (20);
         strcpy (jack.name, "Jack");
         jack.age = 17;
         jack.phone = 5551212L;
  12.  What is the name of one I/O routine, other than fscanf(), that can you use to read in an entire line as a string?

         fgets(), gets()
    
  13.  If p is of type char*, what does the expression p == "hello" actually test?

    Whether p has been set to point to the exact storage location used to hold the literal string "hello". If p points anywhere else, even to another string "hello", the test returns false.

  14.  Suppose you want to be able to declare variables of type string (for example, string s,t;). Write the one statement that defines string to mean the same thing as char*.

         typedef char *string;

     

    Short answer:

  15.  Briefly, what is the difference between declaring a variable and defining a variable?

    Declaring says you have such a variable, and what its type is; defining does this, but also allocates storage for the variable. Note: neither of these has anything to do with assigning a value to the variable.

  16. Briefly describe two kinds of errors that could cause a program to crash with a bus error or a segmentation fault.

    Going outside the bounds of an array; following a null or illegal pointer;
    Using an array index that's negative or too large.
    Putting a string in a storage area that's too small for it .
    Forgetting the & in a scanf() function call (or in any function call where a pointer is expected).
    Putting in a * or & where it doesn't belong.
    Following a pointer that's null, not properly initialized, or freed too soon.


  17.  Why is it important to use meaningful variable names?

    To help you understand what the code is doing, particularly after you haven't looked at it for a while.



  18.  In what case does your instructor suggest that you do not use a meaningful variable name?

    It's a good habit to use i as the index of a loop (j and k for nested loops). Loop indices are typically not used elsewhere, so they are "throwaway" variables, and the consistent naming makes it easier to remember what to use for an index inside the loop.


  19.  What are magic numbers, and why should you avoid them?

    Numbers appearing in your program that could just as easily be replaced by named, defined constants. You should avoid them in order to make your program easier to understand and easier to change.

     

  20. [No points] What aspects of C do you find especially troublesome and would like to have discussed more in class?

              Pointers and strings, with structs a distant third.