next up previous
Next: About this document

centering17

name:

  1. (10 pts) Fill out the questionaire about the textbook distributed with this exam, and turn it in along with your exam when you are finished.
  2. (5 pts) Give the UNIX command needed to copy the file example.c from the examples directory of user jill27 to your current working directory.
  3. (10 pts) Describe at least two situations in which the following program will fail (crash or do something unexpected).
    #include <stdio.h>
    #define MAX 21
    
    main () {
    
    FILE *infile;
    char word[MAX];
    
      infile = fopen("in.dat", "r");
    
      fscanf(infile, "%s", word);
      printf("%s", word);
      fclose(infile);
    }
  4. (5 pts) Give the UNIX command needed to run the executable a.out in such a way that it treats the contents of the file in.dat as if they were being typed in from the keyboard.

    (5 pts) In the following program, which method of reading a sentence from the keyboard is preferred and why?

    #include <stdio.h>
    #define SSIZE 81
    
    main () {
    
      char sentence[SSIZE];
                    
      printf("Enter a sentence : ");
      /* method one */
      gets(sentence);
      /* method two */
      fgets(sentence, SSIZE, stdin);
    }
  5. (5 pts) What will the following program print when it is run?
    #include <stdio.h>
    
    void inc2(int i, int *j) {
    
      i++;
      *j++;
    }
    
    main () {
    
      int a = 5, b = 10;
    
      inc2(a, &b);
      printf("a: %d and b: %d.\n", a, b);
    }
  6. (10 pts) In the following code fragment:
    int *arr;
                          
      arr = (int *) malloc(10 * sizeof(int));
    
      arr[3] = 5;
    Why is it legal to refer to arr as if it were declared as an array?
  7. (5 pts) In the following code fragment:
    int *ptr;
    
      ptr = (int *) malloc(sizeof(int));
    Why is it necessary to put (int *) before the call to malloc?
  8. (15 pts) Given the definition:
    #define ROWS 5
    #define COLS 10
    
    typedef int table[ROWS][COLS];
    Write a C function that takes a parameter of type table and returns the sum of all of the integers contained in the table.
  9. Given the definitions:
    #define SIZE 15
    
    typedef struct {
      double x;
      double y;
    } point;
    
    typedef point pointlist[SIZE];
    1. (10 pts) Write a C function that takes a parameter of type pointlist and initializes both fields of each struct in the parameter to be 0.0.
    2. (15 pts) Write a C function that takes a parameter of type pointlist and writes the contents of that parameter to file out.dat, one point per line in format: (x, y). That is, after your function has been called, each line of out.dat will contain the x and y fields of a point in parenthesis, separated by a comma. You can assume that stdio.h has been included and that your function will be able to open file out.dat correctly.
    3. (5 pts) What error does the following function definition contain?
      void incpoint(point *apoint) {
      /* this function updates both fields of a point by 
         incrementing both by 1.0 */
      
        apoint.x += 1.0;
        apoint.y += 1.0;
      }
    4. (5 pts) Given the declaration:
      pointlist somepoints;
      Write the call to scanf that would be needed to read a new value into the x field of the third point in somepoints from the keyboard.
    5. (5 pts) Given the declaration:
      point apoint;
      What is the type of the expression:
      apoint.x + apoint.y * 3;
  10. (5 pts) Why is it necessary to include the appropriate header file (for example, stdio.h) to use library functions in C?
  11. (5 pts) Give a UNIX command (or commands) that will show a listing of the contents of the current directory, one page at a time.
  12. (5 pts) What does the following program print?
    #include <stdio.h>
    
    void inc(void);
    
    main() {
      int x = 7;
    
      inc();
      printf("x is: %d\n", x);
    }
    
    void inc(void) {
      int x;
    
      x++;
    }
  13. (10 pts) What logical error does the following program contain?
    #include <stdlib.h>
    
    main() {
      int i;
      int *ptr;
    
      ptr = &i; 
      free((void *) ptr);
    }
  14. (5 pts) What does the following program print?
    #include <stdio.h>
    #include <stdlib.h>
    
    main() {
    int i;
    int *ptr;
    
      ptr = &i;
      *ptr = 4;
      i = 3;            
      printf("*ptr is: %d\n.", *ptr);
    }
  15. (5 pts) Describe the value of the array variable arrayOfInts after the following declaration.
    int arrOfInts[10] = {0};
  16. (5 pts) Why is it useful to keep track of a list by keeping the address of the first element of the list, rather than the first element itself?



next up previous
Next: About this document

Tim Wahls
Thu Oct 10 14:44:18 EDT 1996