CS 354, version A
Fall 2011
Name:___________________
Section:________________
ID:___________________
Exam 1
No electronic devices may be used while taking this exam. No calculators, no cell phones. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes.

Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used. Partial credit will be given based on work shown.

Exam Score
Q1 = _____ / 20
Q2 = _____ / 20
Q3 = _____ / 9
Q4 = _____ / 8
Q5 = _____ / 11
Q6 = _____ / 32
Total = _____ / 100









Question 1 (20 points)
The following C program is to be compiled on one of the CS department's instructional Linux machines


#include <stdio.h>
#include <stdlib.h>

int main(                                                ) {
   int i;
   int converted;

   for ( i = 0; i < argc; i++ ) {
      converted = atoi(*(argv + i));
      printf("argv[%d] is %d\n", i, converted );
   }

   return 0;
}

Part A (8 points) The parameters to main() are missing. Provide these parameters in the space provided on the code fragment.

Part B (12 points) With the correct parameters, this program is to be compiled with: cc -o testarg testarg.c
Then, this program is invoked with the command line
    testarg  99 4 500
What is the output of this program?




Question 2 (20 points)
Name the four segments of memory provided to each executing program:









Question 3 (9 points)
Name the 3 types of shift operations:







Question 4 (8 points)
An assembly language instruction is to be used to selectively set the middle two bytes of a 32-bit variable called fourbytes. The instruction to do this operation is

    or  fourbytes, mask, fourbytes
Give the value of mask in hexadecimal.





Question 5 (11 points)
Write the smallest fragment of C (declarations and) code that you can think of that contains a buffer overflow programming error.





Question 6 (32 points)
Consider this C program. It compiles without warnings or errors.

int a(int *x, int *y, int z) {

  z = *y - 2;
  *x = 707;
  return (*y + z);
}


int main() {
   int j, k, l, m;
   int *ptr;

   j = 12;
   ptr = &k;
   *ptr = 32;
   l = j * 2;
   m = a(&j, ptr, l);

   return 0;
}

There are 8 variables declared and used in this program. Trace through the entire program, and show the value of every variable, as well as how each variable changes as the program executes. When a variable's value changes, draw a / over its current value, and then write the new value of the variable. Use arrows to show pointer values.

         x                           j


         y                           k


         z                           l


                                     m


                                     ptr