CS 354, version A
Spring 2011
Name:___________________
Section:________________
ID:___________________
Exam 1
No electronic devices may be used while taking this exam. Examples of devices not allowed are calculators, pagers, cell phones, wrist calculators/computers, laptop computers, pocket computers. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes. The notes may be on both sides of the paper.

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 = _____ / 15
Q2 = _____ / 20
Q3 = _____ / 10
Q4 = _____ / 10
Q5 = _____ / 15
Q6 = _____ / 30
Total = _____ / 100









Question 1 (15 points)
From assignment 1, write a mathematical expression for the integer value represented by the command-line argument bcy.



Question 2 (20 points)
The following C program is to be compiled on one of the CS department's instructional Unix machines with the command: cc -o myargvtest argvtest.c

#include <stdio.h>

int main(int argc, char *argv[]) {
   int i;

   printf("argc is %d\n", argc);

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

   return 0;
}
This program is invoked with the command line
    myargvtest CAT in hat
What is the output of this program?


Question 3 (10 points)
An assembly language code fragment is to be used to selectively clear bits 4, 5, 20, and 31 of a 32-bit variable called X. Bits are numbered from the right, starting at 0. The instruction to do this operation is

    and  X, mask, X
Give the value of mask in hexadecimal.





Question 4 (10 points total)
A C compiler must produce assembly language code for the C source code

    int  i;
    char c;

    i = (int) c;
Assume that a char takes one byte, and that an int takes four bytes. What operation should the compiler utilize to do the conversion?







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







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

#include <stdio.h>
#define  MAX  10
int main() {
   int *ppow2[MAX];
   int pow2[MAX];
   int i;
   int tempint;

   for ( i = 0; i < MAX; i++ ){
      *(ppow2 + i) = pow2 + i;
   }

   tempint = 1;
   for ( i = 0; i < MAX; i++ ){
      *(pow2 + i) = tempint;
      tempint = tempint * 2;
   }

}

Fill in known values of the two arrays on the diagram as they would be just before this program exits. Use arrows to show pointer values, since you do not know what the actual values of addresses would be.



           -------------------------------------------------------------
     pow2  |     |     |     |     |     |     |     |     |     |     |
           -------------------------------------------------------------

           -------------------------------------------------------------
    ppow2  |     |     |     |     |     |     |     |     |     |     |
           -------------------------------------------------------------