Sample Questions from Dr. Tiernan’s CSE1320 tests

 

(Hard questions, weird questions, and typical questions. Some questions are all three.)

Correct answers will be highlighted in green. Comments about the questions will be given in blue.

 

Multiple choice examples:

This question is checking to see if the student understands not just what a linked list is – but when a linked list’s characteristic (easy to expand and shrink, not well suited for significant access to random stored data elements) make it a good choice for a programming problem.

1. When is a linked list NOT an efficient data structure?

A)  If the data must be sorted or regrouped in some way after storing

B)  If the amount of data to be used is unknown and can vary widely

C)  If the data is stable and known and never regrouped

D)  If there is no need to independently access many of the data items.

 

This question is partially a vocabulary question and partially a question of understanding definitions.

2.   What are we considering when we determine which functions can see and use a particular variable?

A)  storage class

B)  scope

C)  flow of control

D)  prototype declaration

 

Pretty straightforward question about using unions – a type of data structure.

3.   Unions can contain elements of type

A)      void

B)       struct

C)      function

D)      real

 

This question requires the student to correctly interpret code and to trace the execution of a program fragment. It specifically checks the student’s knowledge of operator precedence and the increment and decrement operators.

4    After the execution of the following code fragment, what will be the values of a, b & c?

                                                            a = 4;   b = 5;  a++;

                                                            c = ++b * a++; b++;

                                                            c += --a * b--;

A)  a = 5, b = 4, c = 60

B)  a = 4, b = 5, c = 65

C)  a = 6, b = 5, c = 60

D)  a = 5, b = 6, c = 65

 

Checks knowledge of preprocessing (versus general compiling) and understanding of the use of preprocessing commands for simplifying program development.

5.   Which of the following utilities is controlled by preprocessor directives and is

      extremely useful for debugging?

A)  calloc                                     B)  free

C)  IFNDEF                                D)  INCLUDE


 

6.   A fencing pool consists of 6 fencers. Each fencer fences a 5-point bout with all the other fencers, so each fencer has 5 bouts. A bout is recorded by noting the number of touches made by each fencer against the other. A partial example would be Pool #4 with fencers Williams (Number 3), Patel (No. 4), Nguyen (No. 5) and three others. Patel gets 5 touches against Williams and 2 against Nguyen. Nguyen gets 5 touches against Patel and 5 against Williams. Williams gets three touches against Nguyen and 2 touches against Patel. These are the results for 3 (Patel-Williams, Patel-Nguyen, Williams-Nguyen) of the 15 bouts that will come out of Pool #4.

 

A.  Give the declaration for a three-dimensional array that contains a set of data for seven fencing pools.

 

This question requires students to interpret a problem definition (fencing pools) and then define a data structure that matches the problem. Obviously, this is tough in a time constrained test situation if a student is prone to distress when they encounter an unexpected example/situation.

The answer below is a correct solution but it is by NO means the only solution that works.

int pooldata[8][7][7];     //[8 pools (ignore all 0s)][Fencer A gets points against][Fencer B]

                                          // where A is an element from{1,2,3,4,5,6} and

                                          // B is an element of the set ({1, 2,3,4,5,6} – A)

                                          // ex: value at pooldata[4][4][5] = 2 because in Pool #4,

                                          // Fencer #4 Patel got 2 touches against Fencer #5 Nguyen.

 

B.   Given the array from part A, write a printf statement that will print the results for the bout between fencer number 2 and fencer no. 6 in Pool 1.      {5}

 

This is just using the definition from above – but if a student didn’t define it right either a) they won’t be able to do this or b) they’ll see it now and go back and fix part A.

The answer below is a correct solution but it is by NO means the only solution that works.

int pool = 1, fenc1 = 2, fenc2 = 6;

printf(“In Pool #%d, in the bout between Fencer #%d and Fencer #%d, Fencer #%d scored %d touches and Fencer #%d scored %d touches.”, pool, fenc1, fenc2, fenc1, pooldata[pool][fenc1][fenc2], fenc2, pooldata[pool][fenc2][fenc1]);

 

7.   Define the following:                                                                                                    

A.  Give a typedef that allows a user to store integers from the range –210 to 210-1 on a machine where short gets 1 8-bit byte, int gets 2 bytes, and long gets 4 bytes. Use the smallest size possible for the built-in type that the typedef is based on.                              

 

This has a bunch of points to it. Student must figure out the size of an integer with value -210 in bits (12 bits), then figure out how many bytes are needed to hold that many bits when bytes are 8 bits in size (2 or more bytes), and then choose the smallest size possible (int).

typedef          int                  twotothetenth;

 

B.   Show a revised typedef that would be used to port your typedef from part A to a machine where short gets 2 8-bit bytes, int gets 4 bytes and long gets 8 bytes. Use the smallest size possible for the built-in type that the typedef is based on.

 

This is either very easy or very confusing. If A was right, then adjust the choice for the new type sizes (still 2 8-bit bytes => short). If student didn’t get A they won’t get B.

typedef          short             twotothetenth;

 

C.  Show a revised typedef that would be used to port your typedef from part A to a machine where short gets 1 16-bit byte, int gets 2 bytes and long gets 4 bytes. Use the smallest size possible for the built-in type that the typedef is based on.

 

Again, either very easy or very confusing. If A was right, then adjust the choice for the new type sizes (1 16-bit byte => short). If student didn’t get A they won’t get C (or B). Notice that B and C have the SAME ANSWER. =)

typedef          short             twotothetenth;

 

 

8.   Define an enumerated type that contains emerald, opal, garnet, citrine, pearl, amethyst, ruby, peridot, aquamarine, sapphire, turquoise, and diamond. (You may use 2 or 3 letter abbreviations if you want.) Use the enumerated type to indicate that diamond is the birthstone for the fourth month (April), December’s stone is turquoise, the first month’s stone is garnet, ruby is July’s stone, amethyst follows garnet, turquoise follows citrine which follow opal which follows sapphire which follows peridot, aquamarine is before diamond and emerald is after diamond.                                                      

 

OK, this is a lot of work but basically just a logic puzzle with a bit of C knowledge and real world info thrown in. The C knowledge is in the enum syntax and in the assignment of values to the gemstones using the default values from the enum ordering where the first element equates to 0 and so on. The values could also be assigned to each gemstone.

eme, opa, gar, cit, pea, ame, rub, per, aqu, sap, tur, dia

birthstones

dia = Apr = 4                   ame = gar + 1 = 2

tur = Dec = 12                 tur = cit + 1 = 12

gar = Jan = 1                    cit = opa + 1 = 11

rub = Jul =7                     opa = sap + 1 = 10

aqu = dia – 1 = 3             sap = per + 1 = 9

eme = dia + 1 = 5            per = 8

6 = pea

enum BIRTHSTONES {gem, gar, ame, aqu, dia, eme, pea, rub, per, sap, opa, cit, tur};

 

9.   Explain how the use of something like void func(void *a, void *b); as a function prototype increase the flexibility of the execution of our software.                             

 

      Does the student recognize the special type void in this prototype and understand why they have learned about void parameters?

      void types allow the use of any parameter types to pass into func and return from it. This makes the function extremely flexible. For example, a sorting function with void parameters allows you to sort ints, floats, or chars with the same function rather than writing three separate functions.


10. How can a do-while loop be simulated with the for loop?

 

      Creative thinking on the student’s part is called for here. Can they look at the characteristics of one loop and replicate it within the structure of another type of loop?

      The primary characteristics of the do-while loop are that it always executes at least once and it has a true/false test at the bottom of the loop. Thus to duplicate this functionality with a for loop, the for condition must have an infinite loop that will be terminated by something within the loop, giving at least one execution and a true/false test that may terminate after the first iteration.

 

11. What is the mathematical purpose of the function recurse?

 

int recurse(int l, int h) {

      if lh

                  if l = h

                  return l;

                  else return 0;

      else

                  return recurse(l +1, h -1)+ l + h;

}

Can the student understand what a piece of code is accomplishing? This includes understanding the nature and purpose of the recursive call and when the recursion stops.

The function recurse takes two integer inputs, l and h, checks to make sure l is less than h, then recursively calls itself with l + 1 and h – 1. The return value then is the sum of the two inputs of each recursive call until the value of l = h or l > h. When l = h the value of l is added to the sum. Therefore the function adds the values from l to h which is the summation of the numbers from integer l to integer h or (h + l)(hl + 1)/2.

 

12. Using the math library functions and operator precedence, write the quadratic formula given below as a short set of assignment statements which will get both of the roots. Make sure to properly declare all the variables needed.         

                                          x = [-b +or- sqrt(b**2 - 4ac)]/2a        (sorry this isn't pretty,)

This is very easy if a student understand how to translate the mathematics into C. Not at all easy if the quadratic formula stumps them.

float a, b, c, x1, x2;

x1 = (-b + sqrt(b*b – 4*a*c))/(2 * a);

x2 = (-b - sqrt(b*b – 4*a*c))/(2 * a);

 

 

13. Write a recursive function using the function declaration for oddsum below to find the sum of the first k odd integers starting with 1, e.g. if k is 3, then sum up the first three odd integers 1 + 3 + 5.  

 

This question actually has a number of challenges in it -first, recursion; second, k as the number of integers, third, using only the odd integers. This is a case where a loop would probably be an easier choice but the question needed to test recursion. The trick here is to remember that k indicates the number of ODD numbers to add together, i.e. it is just a counter, not something to be added. The whole thing falls into place if you think of starting with k and going down to 0. The odd numbers are 2k-1 each time until k = 0 when the recursion should stop.

 

      int oddsum(int k) {

            if (k >= 1)

                        return (oddsum( k – 1 ) + (2 * k – 1));

            else

                        return 0;

      }

 

XC. Finish the following limerick in correct limerick form WITHOUT using the crude and obvious rhyme, i.e. make the last line rhyme and have the same rhythm as the first and second lines and don’t use any tacky words!

{Limericks use five lines where the first, second and fifth line have the same scan and the same rhyme and the third and fourth lines have the same scan and rhyme and are shorter than the other three.}                                                                   

 

This kind of thing is ONLY given as extra credit and I give some credit for ANY answer at all. =) I gave the explanation of the limerick form to help those not familiar with it (although I am not sure that it really helped anyone.) A silly question like this sometimes helps people lighten up on other parts of the test. Maybe a completed limerick as an example would have helped…

 

      There once was a CSE class

      Whose tests I just barely could pass.

            I sorta learned C,

           with pointers, WOW--EE!,

                                                                                                                                               

 

      There once was a CSE class

      Whose tests I just barely coud pass

            Alg-rithms and structures

            Were causing me fructures

      And the tests were a pain in the –ss. <- This word would be the crude and

                                                                             obvious rhyme.

 

 

 

 

 

 

ÿ