CS 240 Fall 2002 Exam One

(Closed Book, Closed Notes)













First Name: ____________________________________


Last Name: ____________________________________


Student ID: ____________________________________




Part One : Multiple Choice

1. The correct way to use the define directive to create a symbolic constant called TRUE, which has a value of 1 is:

        (a) int TRUE = 1;
        (b) #define TRUE = 1
        (c) #define TRUE = 1;
        (d) #define TRUE 1
        (e) #define 1 TRUE



2. The operator used to derefrence pointers is:

        (a) %
        (b) &
        (c) *
        (d) ^
        (e) +



3. What do you expect the output of the following code fragment to be?

int var = 5 % 6;
switch(var)
{
  case 0:
    printf("case 0 = ");
  case 1:
    printf("case 1 = ");
  case 3:
    printf("case 3 = ");
  case 5:
    printf("case 5 = ");
  default:
    var = 0;
}
printf("%d\n", var);

        (a) case 0 = 0
        (b) case 1 = 1
        (c) case 3 = 3
        (d) case 5 = 5
        (e) None of these



4. In C arrays are:

        (a) Always column major
        (b) Always row major
        (c) Can be either column major or row major depending on how they are declared
        (d) Neither column major nor row major
        (e) Always stored in a file



5. In lab two you were given the file broken.c and asked to correct it. The handout that accompanied lab two contained a list of different types of errors and ways to correct them. Based on what you learned from completing that lab what type of error is shown below?

Undefined                       first referenced

 symbol                             in file

  print                               /var/tmp/cctDh2yF.o

ld: fatal: Symbol referencing errors. No output written to a.out

        (a) A compiler error
        (b) A linker error
        (c) A run time error
        (d) A message to indicate that a file could not be opened
        (e) None of these



6. What do you expect the output of the following code fragment to be?

char * ptr1 = "i love cs240";
char * ptr2;
*ptr2 = *ptr1 + 4;
printf("%c\n", *ptr2);


        (a) The letter 'm'
        (b) The letter 'v'
        (c) The ASCII value of the letter 'm'
        (d) The ASCII value of the letter 'v'
        (e) This code will likely cause a segmentation fault



7. A pointer variable always contains:

        (a) The data element stored at a location in memory
        (b) The address of a memory location
        (c) The address of the first element in an array
        (d) The address of some dynamic memory allocated by malloc(...)
        (e) None of these



8. What do you expect the output of the following code fragment to be?

struct foo {
   int a;
   char b;
   float c;
};

int main() {
   foo.a = 400;
   foo.b = 1025;
   printf("a=%d, b=%d, c=%d\n", foo.a, foo.b, foo.c);
}


        (a) a=400, b=1025, c=0
        (b) a=400, b=-260, c=0
        (c) We cannot determine the output because the wrong format specifiers were used in printf
        (d) We cannot determine the output because foo.c is not initialized
        (e) There is no output because the code does not compile



9. Imagine that you have already written a very efficient function for sorting arrays of integers. Your function is called super_sort. It takes an integer array as an argument and returns void. After reading in some input from the user you now have an all 42 elements of the integer array my_array filled. Now all you need to do is call super_sort on my_array. How should you pass the array to the function in order to get the array back in sorted order when the function returns.

        (a) super_sort( &my_array[42] );
        (b) super_sort( my_array[] );
        (c) super_sort( my_array );
        (d) super_sort( *my_array );
        (e) None of these



10. What do you expect the output of the following code fragment to be?

int* P;
int* Q = NULL;
int X = 10;
int Y = 20;
P = &X;
Q = &Y;
*P += 7;
*Q += 3;
X += 5;
P = &(*Q);
*P *= 10;
Y += 4;
printf("X=%d, Y=%d\n", X, Y);


        (a) X=220, Y=27
        (b) X=32, Y=27
        (c) X=22, Y=234
        (d) X=15, Y=24
        (e) X=22, Y=220



11. If you have the array:
int array[] = { 5, 10, 15, 20, 25 };
and you wish to use scanf to put a new value in the last element of the array, which code fragment would be most appropriate?

        (a) scanf( "%d", array[4] );
        (b) scanf( "%d", array[5] );
        (c) scanf( "%d", &array[4] );
        (d) scanf( "%d", &array[5] );
        (e) scanf( "%d", *array[4] );



12. Which of the following statements is true regarding the array declaration:

int numbers[4] = { -1, 0, 1 };

        (a) It is legal syntax
        (b) It will cause a compiler error because there are 3 initializers but only 4 array elements
        (c) It will cause a compiler warning because there are 3 initializers but only 4 array elements
        (d) The compiler will change the size of numbers to 3 because the last element is not used
        (e) Both (a) and (d) are correct



13. Which of the following code segments will always execute the function baz() at least once. Assume that lexp represents some logical expression.

        (a) while( lexp ) { baz(); }
        (b) if( lexp ) { baz(); }
        (c) for( ; lexp; ) { baz(); }
        (d) do { baz(); } while( lexp );
        (e) None of these



14. Given the code:

struct bar {
   int x;
};

struct bar B = { 10 };
struct bar* ptr = &B;


Which of the following allows you to access the number stored in B.x?

        (a) (*ptr).x
        (b) ptr.(*x)
        (c) ptr.x
        (d) *ptr->x
        (e) ptr->*x



15. Which of these standard library functions is most appropriate for reading from a binary-mode file?

        (a) fscanf
        (b) fgets
        (c) fread
        (d) fwrite
        (e) fprintf




Part Two : Short Answer

16. Given the following code:
#define NINE 3*3
int x = 12 % NINE * 3 + NINE / 3;

The value stored in the variable x is ______________________



17. In lab three you learned that there was a relation between binary digits and octal digits.

The binary representation of the octal digit 5 is ______________________



18. Declare a pointer to a float and use the appropriate standard library function to assign it to point to 13 dynamically allocated floating point variables.

____________________________________________________________________________



19. What do you expect the output of the following code fragment to be?

union superhero {
   char hero_name[50];
   char normal_name[50];
};

union superhero h1 = { "Spiderman" };
strcpy( h1.normal_name, "Peter Parker" );
printf("%s is really %s\n", h1.hero_name, h1.normal_name);


Output = ______________________________________________________________________________



20. Write a function prototype for a function called xyzzy that takes two integers and a charachter string as arguments and returns a float.

____________________________________________________________________________



21. Given the following code:

int x, y;
for(x=0; x<10; x++)
   for(y=-5; y; y++)
      qux();

How many times will the function qux() be called? _______________________



22. What keyword is used to tell the compiler to skip the remaining statements to be executed within a loop and jump immediately to the next iteration of the loop? _______________________



23. Given:
char str[10] = "cs240";
printf("(%d, %d)", sizeof( str ), strlen( str ));


The output is:

________________________________________



24. What keyword is added to the declaration of a local variable to instruct the compiler to save the value of the variable between function calls _______________________



25. You may have a global variable named x and a variable named x in the main function because they are each in a different _______________________



26. Make a call to the appropriate standard library function to open the file "file.dat" for reading.

____________________________________________________________________________



27. Given the following function prototype, declare a pointer variable funptr that can be used to store the address of this function.

int fun( float*, char );

____________________________________________________________________________



28. Write a printf statement that will print the floating point variable money with exactly seven digits before the decimal and exactly two decimal places after the decimal.

____________________________________________________________________________



29. What should you type at the command line to run the executable a.out and have it get all of it's input from the file "f.in"? ____________________________________________________________________________



30. Given:

int i = 256;
char c = (char) i;


The value stored in the variable c is _______________________




Part Three : Programming

Ever since the dawn of role playing games in 1974 there has been a significant intersection between the set of all programmers and the set of all role players. Most all role playing games involve rolling dice with a bizarre number of sides (12 sided dice, 20 sided dice, etc.) A die with n sides has sides labeled 1 through n. Fortunately in project one you worked with the standard C functions for generating random numbers. Write a function that takes three arguments random_seed, num_sides, and num_rolls. Your function should seed the random number generator, and then use it to simulate rolling a die with num_sides num_rolls times. Print out the result of each of the rolls.













Matrices are very useful data structures for many scientific and mathematical applications. Matrices are frequently represented as two dimensional arrays. However, in some instances you do not need a full two dimensional array. Interestingly in many such cases you only need half of the two dimensional array. The problem is that the half you need is divided across the diagonal. Write a function called create_diagonal_matrix that takes an integer num and generates a diagonal matrix of height num. The zeroith row of your matrix should have 1 element, the next row should have 2 elements, and so on up to the n-1 st row which should have n elements. You should return a pointer to your matrix. Hint: you will need to dynamically allocate the matrix.