CS140 Midterm 1

Spring 2005

Instructions

  1. Write your name clearly at the top of the exam and on each page of the exam.

  2. You must answer all the questions on the exam. The exam has 5 problems. Make sure that you have all 5 problems.

  3. Write your answers on the exam pages and hand in the exam when you are finished.

  4. You have until the end of the class period to finish the exam. When I call time you must immediately drop your pen/pencil and hand in your exam.

  5. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.

  6. The exam is closed note, closed book, closed neighbor.

  7. Good luck!


  1. (30 points )
         a. strdup            b. strcpy         c. strchr         d. type cast
         e. strcmp            f. extern         g. struct         h. typedef
         i. union             j. void *         k. char *         l. #include
         m. malloc		  n. free           o. scanf          p. printf
         q. stdin             r. stdout         s. stderr         t. fscanf
         u. fprintf           v. sscanf         w. sprintf        x. hexidecimal
         y. octal             z. decimal        aa. binary        bb. compiler
         cc. linker           dd. object file   ee. executable file
         ff. argc             gg. argv          hh. data encapsulation
         ii. bus error        jj. segmentation violation          kk. Jval
         

    For each of the following questions choose the best answer from the above list. You may have to use the same answer for more than one question:

         
    1. ____________ A data type that represents a generic type.
    2. ____________ A function that copies a string from a source to a destination and which assumes that the source already has enough memory to accommodate the copied string.
    3. ____________ What the -o option causes gcc to produce.
    4. ____________ The result when you try to access a memory address that is not divisible by 4.
    5. ____________ A system function that you can use to convert a string to an integer.
    6. ____________ Another name for a base-8 number.
    7. ____________ The variable that represents command line arguments as an array of strings.
    8. ____________ The I/O stream you should use when writing error messages to the user.
    9. ____________ The keyword used to specify a shorter, more readable name that can be used for a struct.
    10. ____________ The name of the operation that converts a (void *) to a (char *). For example, char *s = (char *)v;

  2. (8 points) Write a declaration and a malloc statement that will declare a variable that points to an array of n integers, where n is a variable containing the number of integers the array should hold.
    
    
    
    
    
    
    
    
    
    
  3. (8 points)
    1. Declare a union named Item that consists of a char *, a character array of 20 characters, and an integer array of 3 integers. Name the fields whatever you like.
      
      
      
      
      
      
      
      
    2. Assuming that a char * occupies 4 bytes, a char one byte, and an integer 4 bytes, what is the size of the union that you just declared?
      
      
      	

  4. (24 points) Put a check mark next to any of the following statements that is illegal. Put an 'X' next to any of the following statements that might eventually cause a segmentation violation or bus error. Consider each statement in isolation and assume that all variables have already been initialized to valid values. If there is more than one statement on a line consider the statements as a group (e.g., if the combined effect of the statements might be to eventually cause a segmentation violation, then put an 'X' next to the statements). If you mark a statement as incorrect and want partial credit if you are wrong, provide an explanation. If your explanation is good enough, you will get some partial credit.

           char name[20];
           char first_name[10];
           char last_name[20];
           char *str_ptr = (char *)malloc(sizeof(char) * 5);
           void *void_ptr;
           IS is = new_inputstruct(0);
           Jval j;
           char c;
    
           _________  scanf("%s", str_ptr); // assume the string to be read is less than 5 chars
    
    
           _________  str_ptr = is->fields[3];
    
    
           _________  j.s = name;
    
    
           _________  name = (char *)j.v;
    
    
           _________  str_ptr = strdup(first_name); strcat(tr_ptr, last_name);
    
    
           _________  str_ptr = (char *) void_ptr;
    
    
           _________  scanf("%d", j.i);
    
    
           _________  c = name[3]; 
    
    
           
  5. (30 points) You are to write a function that given two sorted lists of integers, L1 and L2, prints the integers that are common to both lists. For example, if L1 contains the integers 1, 3, 6, 10, 12 and L2 contains the integers 2, 3, 10, 12, 15 then your program should print:
    	3
    	10
    	12
    	
    You should assume that you have the following definition for a list node:
    	struct node {
    	    struct node *next;
    	    int value;
    	};
    	
    The lists do not have sentinel nodes so the pointers passed to your function point directly to the first nodes on their respective lists. The last element of each list will point to null (0). If you want full credit your solution should take advantage of the fact that the two lists are sorted and traverse each list only once. If you cannot figure out how to do that, you can write a program for 26 points that traverses one list once and the other list multiple times.
    	void print_common_elements(struct node *L1, struct node *L2) {