Spring 2002 Test #3

CSE1320                                                                                 Sections 003 (2:30) and 501 (5:30)

Tuesday, April 2, 2002                                                                                          Dr. Carter Tiernan

 

Name:                                                                                             Section:                                             

Student ID:                                                                                    

 

Instructions:

 

1.   Fill in your name, student ID, and section above.

 

2.   This is a closed book, closed notes test.

 

3.   The test is worth a total of 100 points. The value for each question is given either at the top of that section of questions or in curly braces to the right hand side of the question. There are extra credit questions at the end of the test worth an additional 10 points total.

 

4.   If you feel stuck on a question, go on to the next questions and come back later to the challenging one. Try to go all the way through the test before spending a lot of time on a single queston.

 

5.   Read the entire question before answering. Every part of the question is important.

 

6.   Show any work or partial work on a question so that you may be able to receive partial credit.

 

7.   NO CHEATING!

 

 

 

 

Do NOT begin this test until Dr. Tiernan tells you to.

 


Multiple choice questions are worth three {3} points each. Please circle the letter indicating your choice for the answer.

Given the following code fragment, answer questions 1 – 5. (Assume this code is part of a larger valid (compilable) C program.)

static float alpha;

int fgreek(char *, float *, char); /*prototype declarations */

int fenglish(char *, int *, char);

int main(int argc, char *argv[]) {    /*main routine*/

      register float gamma;

      int eta;

      float *kappa;

      int *lambda;

      int (* flang[10])(char *,void *, char);

/*assume some valid C code is here */

      eta = fgreek(“Socrates”,kappa, ‘h’);

      gamma = fenglish(argv[1], lambda, argv[2][0]);

 

1.   The variable flang is:

A)  a pointer to a function that has three input parameters and returns an array of ten ints

B)  a function with three input parameters that returns a pointer to an array of ten ints

C)  an array of ten pointers to a function that has three input parameters and returns an int

D)  a pointer to an array of ten functions that take three input parameters and return int

 

2    In the call to fenglish, what is argv[2][0]?

      The variable arv[2][0] is:

A)  the first letter of the third element in a global array

B)  the first letter of the third input from the command line

C)  the third element in a global array

D)  the third input from the command line

 

3.   It would be legal for flang[3] to have the address of:

A)  fgreek

B)  fenglish

C)  fgreek or fenglish

D)  neither of the above

 

4.   Which of the variables in the code fragment above is global to its entire file?

A)  kappa                                 C)  lambda

B)  gamma                               D)  alpha

 

5.   Which of the variables in the code fragment could be global to all the program files?

A)  fenglish                             C)  lambda

B)  flang                                  D)  alpha


Use the following code fragment to answer questions 7 - 9

 

      int blimey[3][3][3] = {0};

/*assume some valid C code is here that puts values into blimey from a file */

      int temp = 1;

      int count = 0;

      for (int m=0; m < 3; m++)

            for (int r = 0; r < 3; r++)

                  for (int c = 0; c < 3; c++)

                  if (temp != 0)

                        printf(“Element %d of blimey is %d”, ++count,            

                                      temp = *(*(*(blimey + m) + r) + c) );                    /* A */

 

6.   Rewrite the part of theprintf statement labeled A from the above code fragment to use array subscripts instead of pointer arithmetic to access blimey.       {8 pts}

 

                                                                                                                                                /* A */

 

 

 

7.   If one of the lines of output from the code above is:                                                      {9 pts}

 

            Element 23 of blimey is 7

 

      give the array subscript notation for the element of blimey that has the value of 7

 

       blimey[ ][ ][ ] = 7

 

 

 

8.   If an element of blimey equals 0, what happens to all the following elements in blimey?{13}

 

 

 


9.   Write the necessary declarations and statements to connect an existing physical file named testdata.dat to a program for reading and writing. Declare only those things needed in order to work with the file. ASSUME that stdio.h is already included.        {12}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10. Write the necessary declarations and statements to create and initialize two variables lines and at. The variable lines should be an array of 16 strings (where a string is declared as a pointer to character). The variable at should be a pointer to a string. Both at and all the elements of lines should be initialized to NULL.       {11}

 

 

 


11. Using the declarations you wrote for questions 9 and 10, write a code fragment to read in 16 lines of input from the file testdata.dat and store them using the pointers in lines. Each line will be no more than 80 characters long. The file may have less than 16 lines in it. You may use the string, block, or formatted input commands but do NOT use the character input command to read the data from the file. (Do not redeclare the file or the array) {17}

      ((Even if you are not sure that your declarations were correct, ASSUME they are and try to answer this question. Partial work will at least receive some credit.))

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12. Using the code fragment and declarations above, use the variable at to access the last six strings of data (not NULL) in array lines and print them to the standard output (still using only the variable at.)                                                                              {15}

      ((Even if you are not sure that your declarations were correct, ASSUME they are and try to answer this question. Partial work will at least receive some credit.))


 

Extra Credit questions (Each question worth two points)

 

XC1.   Which of the following declares a variable tap that is a three-dimensional array of integers?

 

A)  short tap[5][4][2];

B)  long array[3][6][4];

C)  float tap[7][5][9];

D)  char tap[3][7][2][2];

 

 

XC2.   Which of the following are commands which enable a programmer to randomly access the data in a file?

 

A)  fseek and fread

B)  ftell and flocate

C)  fseek and ftell

D)  fspeak and fspell

 

 

XC3.   If argc and argv are declared as parameters for the main routine, then in the simplest case, the value for argc is:

 

A)  0

B)  1

C)  a string of the execution command for the program (e.g. “a.out” for gcc)

D)  NULL

 

 

XC4.   Which of the following is a function that has two parameters that are both floats and returns a pointer to a function that has two integer parameters and returns a pointer to int?

 

A)  int *(*func(int, int))(float,float)

B)  int *(*func)(float, float)(int,int)

C)  int **func(int, int)(float,float)

D)  int *(*func(float, float))(int,int)

 

 

XC5.   Which of the following best describes your reaction to this test? All answers will receive two points. (There is no wrong answer.) J

 

A)  panic

B)  hysteria

C)  relief

D)  other (please describe)