Midterm Exam Name ____________________________________

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 = 5; j = --i;
    printf("%d\n", i>j ? i : j);
     
    i = 0; j = 10;
    while (i < j)
      i++; j--;
    printf ("i=%d, j=%d\n", i, j);
     
    int a[10];
    for (i=j=0; i<10; i++,j--)
      a[i]=j;
    printf ("a[2]=%d\n", a[2]);
     
    i = rand() % 10 / 100;
    printf ("i=%d\n", i);
     
    i = 15;
    printf ("%d %o %x\n", i, i, i);
     
    char *p;
    for (p = "hello"; *p; ++p)
      printf ("%c", *p + 1);
     
    i = 2;
    switch (i) {
      case 3: printf ("three");
      case 2: printf ("two");
      case 1: printf ("one");
      default: printf ("zero");
    }
     
    i = 10; j = 3;
    k = i - j - 2 * j;
    printf ("k=%d\n", k);
     
    i = 1; j = 2;
    printf ("%d\n", i > j);
     
  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  
    & b  
    a && b  
    a % b  
    a += b  
    a != b  
    a >> b  
    a . b  
    a -> b  

     
  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 */
    
    
       /* Open the file for writing */
    
    
       /* Write the message onto the file */
    
    
       /* Close the file and return */
    
    
    
    }
  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.



  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.



  10.  Write a loop that sets the array a[100] to all zeros.





  11.  (a) Declare a struct person to hold a person's name, age, and phone number.






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



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








  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?



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




  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*.



    Short answer:

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







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







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







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







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







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