Spring 2002 Test #2

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

Thursday, February 28, 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.   NO CHEATING!

 


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

 

Given the following declarations, answer questions 1 – 4. (Assume these declarations are within some valid C code.)

 

enum liq_level {empty, quarterfull=25, halffull=50, threeqrtrfull=75, full=100} gas, oil, water;

struct readings {

      float gauge1, dial1;

      int pressure;

      enum liq_level tankA, tankB;

      struct readings *last;

} rdng_ary[10], gas_meter, oil_gauge, water_tank;

struct readings *old_bldg;

struct readings apt8 = {45.7, 16.99, 52, halffull, full, NULL};

old_bldg = &apt8;

 

1)   What would we get if we access old_bldg.last->dial1?

A)  An error would occur.

B)  16.99

C)  NULL

D)  A float whose value is not specified above

 

2)   Which of the following will work correctly in ANSI C?

 

A)  rdng_ary = old_bldg;

B)  quarterfull = oil;

C)  oil_gauge.pressure = full;

D)  apt8.last = old_bldg;

 

3)   Which of the following would assign a value of 50% full to TankA at index number 3 in the array above?

A)  rdng_ary[2].TankA = 0.50;

B)  old_bldg[3]->TankA = halffull;

C)  rdng_ary[3].TankA = apt8.TankA;

D)  rdng_ary[3]->TankA = halffull;

 

4)   To allocate space for water_tank, the computer must allocate enough space to store which of the following?

A)  float, float, int, int, int, address

B)  address, float, float, int, int, int, address

C)  float, float, int, int, int, int, int, int, address

D)  float, int, enum, address

 

 


5.   Rewrite the for loop below using the do-while construct.                                                {10}

 

for( int x = 0, k =0; x < 10; x++, k=x*x)

      printf(“k has the value %d on iteration %d. \n”, k, x);

 

 

 

 

 

 

 

 

 

 

 

 

6.   Given the declarations below, answer parts a), b) and c).

 

int practice( int results[50]);

/* main routine starts here */

int values[50] = {0,1,2,3,4,5,6,7,8,9};

int res_tmp;

res_tmp = practice( values );

/* main routine ends, practice and other functions defined */

 

Answer parts a), b), and c) from inside the function practice,

a)   Access the 10th value of values using array notation. (Just give the notation to access the value. You do not need to make a complete C statement or an assignment.)                   {4}

 

 

 

 

 

 

a)   Access the value at array index 5 of values using pointer arithmetic. (Just give the notation to access the value. You do not need to make a complete C statement.)                      {4}

 

 

 

 

 

 

c)   Assign the value 25 to the 25th element of the array values. (Make this a complete C assignment statement..)                                                                                                                                                  {4}

 


7.   a)   Write a declaration for a union called tempo that can hold an integer molto_accel, an integer andante, an integer allegro, and a float adagio and create a variable sonata of type tempo.                                                                                                               {6}

 

 

 

 

 

 

 

 

 

 

 

 

 

8.   Declare a structure called student_ID to hold the following information: a string for the student’s last name, a string for the student’s first name, a single character for a middle initial, an 9-element integer array to hold a 9-digit student ID number, a date of validation, an integer that will hold the student’s age, and a pointer to another student_ID. Assume that the first and last names cannot be more than 50 characters each in length.                                                 {12}

 

 

 

 

 

 

 

 

 

 

 

 

 

9.   Given the declaration above and the one below,

 

      struct student_ID *scholars;

 

      dynamically allocate enough space so that scholars will be able to store the data of 200 students.                                                                                                                   {7}

 

 

 

 


10. Given the function below,

 

int mystery( int innum ) {

      if (innum < 0) {

            printf(“Invalid input.”);

            return –1;

      }

      else if (innum == 0) || (innum ==1)

                  return innum;

            else

                  return (2 * innum – 1) + mystery( innum – 1 );

}

 

      a)   Describe what kind of function it is.                                                                                   {4}

 

 

 

 

 

 

      b)  Describe in words the kind of mathematical calculation the function is doing.                               {7}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

      c)   Give the value returned by the function for a call of mystery(6).                                  {8}

 


11. Write a switch statement that switches on the value of an integer called sw_test. The first case ( with a constant value of 0) should print any short message and then break. The second (1), third(2) and fourth(3) cases should all set an integer flag to be true (you can assume this variable is declared) and then should break. The fifth(4) case should print “Five is alive!” and then break. The sixth(5) case should set flag to false and then should exit with a value of 128. The seventh case should print “kilroy”.                                                        {14}

 

 


Extra Credit questions

 

XC1.   For the structure defined in problem 8, declare an additional member(s) of the structure student_ID that will hold the following information: undergraduate? (yes/no), international student? (yes/no), and a one digit code for class info [1 is freshman, 2 is soph, 3 is junior, 4 is senior, 5 is masters, 6 is doctoral, 0 is special student, 7 is degreed undergraduate]. Be as efficient as possible in recording this data in the student_ID.

      (Do not rewrite the whole structure, just give the additional member field(s).)              {4}

 

 

 

 

 

 

 

 

 

XC2.   Name and describe three string functions from the C library.                                     {3}

 

 

 

 

 

 

 

 

 

 

 

XC3.   What is the C command that allows extremely unstructured programming and what is possible benefit of using this command?                                {3}