CS 354, version A
Fall 2010
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 = _____ / 12
Q2 = _____ /   6
Q3 = _____ /   6
Q4 = _____ /   4
Q5 = _____ / 20
Total = _____ / 48



Question 1 (12 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>
#define MINARGS 4

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

  if (argc < MINARGS) {
      printf("                                           ", MINARGS - 1);
      return 0;
  } else {
    printf("                                              ", argc - 1);

    for ( i = 1; i < argc; i++ ) {
      printf("                                       ", i, *(argv + i) );

      k = 0;
      while ( *(argv[i] + k) != '\0' ) {
         k++;
      }
      printf("                                              ", k);
    }
  }

  return 0;
}
Fill in the format string of each printf(), such that the program's output will specify what is being printed.


Question 2 (6 points total)
Which of the following 6-bit, unsigned integer arithmetic calculations cause overflow?

     101101
   + 001000
   ----------



     110100
   - 101100
   ----------



     000100
   + 111101
   ----------




















Question 3 (6 points total)
Which of the following 6-bit, two's complement integer arithmetic calculations cause overflow?

     101101
   - 001111
   ----------




     110100
   - 101100
   ----------




     000100
   + 111101
   ----------



Question 4 (4 points)
A 32-bit variable has the value 0x88883331. What is this variable's value (in hexadecimal) after a rotate left by 3 bits?













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

int main() {
   int A[4];
   int B[4];
   int C[4];
   int X;
   int *ptr;

   ptr = B;
   for ( X = 0; X < 4; X++ ) {
      *(ptr - X) = X * 2;
   }
}

Part A (4 points)
This program contains a programming error. Describe the error.



Part B (16 points)
Assume that the arrays are placed in memory as shown on the diagram. Each box represents 1 integer. Fill in known values of arrays A, B, and C on the diagram of memory, as they would be just before this program exits.
           |            |   ^ address 0
           |------------|
        A  |            |
           |------------|
           |            |
           |------------|
           |            |
           |------------|
           |            |
           |------------|
        B  |            |
           |------------|
           |            |
           |------------|
           |            |
           |------------|
           |            |
           |------------|
        C  |            |
           |------------|
           |            |
           |------------|
           |            |
           |------------|
           |            |
           |------------|
           |            |