Spring 2004   Final Exam

CSE1320      Section 501

Tuesday, May 11, 2004    Dr. Tiernan

 

Name:                             Section:                      501           

Student ID:                     Keyword:                   

 

Instructions:

 

1.   Fill in your name, student ID, and section above.  Choose some keyword for yourself that I can use to anonymously post grade information.  If you do not choose a keyword, I cannot post your grade information if needed.

 

2.   This is a closed book, closed notes, NO CALCULATOR 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.   In questions where I have given you code to use, if you find typos, assume that the code is SUPPOSED to work and just indicate what assumption you made for your answer.  This applies in all cases unless the question is specifically about the syntax of the code - in which case there are errors on purpose.

 

5.   I will always try to give partial credit so the more of your work that you show, the more I am able to grade for partial credit if the answer is not entirely correct.  It is to your benefit to show your work on the test.

 

 

6.   NO CHEATING!

 

 


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

 

1.   Which of the following is not in a common header file for an ANSI C program?

 

a.   FILE *

b.   abs()

c.                  INT_MAX

d.   strcmp()

 

 

2.   Which of the following returns the current time as a printable string?

 

a.   ctime()

b.                  localtime()

c.                  printtime()

d.   time()

 

 

3.   Which of the following is not related to dynamic space allocation?

 

a.   calloc

b.   malloc

c.   new

d.   sizeof

 

 

4.   The symbol >> in C++ is the

 

a.   scope resolution operator

b.   stream insertion operator

c.   stream extraction operator

d.   invoking object

 

 

5.   The line below -- would be called a:

            #define ntr(x, y)  abs( x - y )

 

a.   macro

b.   member initialization list

c.   inline function

d.                  conditional compilation

 


 

6.   Give an example of each of the following.           {12 points total ;  2 each}

 

an enumerated type                                                         

an invalid C identifier                                    

an array of pointers                                      

 a C function prototype with a pass-by-reference parameter

 

                                                           

C++ dynamic memory allocation                                                      

opening a file for appending                                                   

 

 

7.   Describe the following in less than two sentences and give an example of each: 

      {12 points total; 3 each}

 

a. Polymorphism

 

 

 

 

 

 

 

b.   Data Encapsulation

 

 

 

 

 

 

 

c. Procedural (or sequential) programming

 

 

 

 

 

 

d.   Strings in C

 


8.   Write a recursive function kdm, with positive int inputs k and m of values less than 1000, which returns one of two types of values:  A positive value which is the result of m operating on k or a negative or zero result if the operation cannot be performed with m and k.  kdm is:

 

                        1 + kdm(k-m, m)                  for k > m

      kdm(k,m) =                   1                  for k == m

                        -1000                  for k < m

 

      Note:  You do NOT need to do error checking on the initial values of k and m.  You can assume they are both positive and less than 1000.

 

      a.   Write the recursive function kdm defined above in C.  {12}

 

 

 

 

 

 

 

 

 

 

 

 

 

      b.   What is the value of kdm( 18, 3 )? {Show your work to get partial credit} {6}

 

 

 

 

 

      c.   What is the value of kdm( 10, 2 )?   {4} 

 

 

 

 

 

      d.      Would the result of kdm( 15, 7 ) be positive or negative and why?      {6}

 

 

 

 

 

 

      XC e.      What mathematical operation is kdm implementing for special cases?      {XC 3pts}

 


9.     Use the following C++ code to answer the questions below:

 

int cherry( long& pit, float stone = 0.35);

 

   //  Assume other valid code and a the beginning of a main function here

         //  assume a class fruit is defined with object red declared

 

int berry = red.cherry (pip);  // yes, pip is correct here

 

   // end of main here

        // assume cherry is defined as a function of class fruit

 

int fruit::cherry (long& pit, float stone)

{

      bool taste;

      cout << “Please enter a 5-digit integer”;

      cin >> pit;

      if ((pit > 9999) = taste)

            double mara = pit * stone;

      if taste return mara;

      else return 0;

}

 

a.   What will the value of berry be if the value of pit entered by the user is 42140? {8}

 

 

 

 

 

b.   Rewrite the function cherry and its function prototype in ANSI C..  (Make the C function as much like the C++ function as possible using C constructs.)    {13}

 

 

 

 


10. Given the following code fragment, answer the questions below:

 

struct ST {

      char *capt;

      int commander;

      float stardate;

      struct ST *nextgen;

      struct ST *old;

      } *spock, *geordi, *dax, *chakotay;

 

struct ST *tolist, *best, *rate;

 

/*  Assume some valid code here */

/*  For each of the four pointers, assume the same initializations as for geordi and that other valid data is stored in the all the members of structs */

      geordi = (struct ST *)malloc(sizeof(struct ST));

      geordi.nextgen = NULL;

      geordi.old = NULL;

 

/*  Now based on a complicated system, the structs are being sorted into a list.  The element to be added to the list is tolist , the beginning of the list is best and the current element in the list that is being compared with tolist is rate.  Part of this sorting code is given below.*/

 

      if (                                      )     /* Line 0 */

      {     tolist->nextgen = NULL;      /* Line 1 */

            tolist->old = rate;    /* Line 2 */

            rate->nextgen = tolist;      /* Line 3 */

 

 

a.   Where is tolist being added to the sorted list?  (i.e.  what location did tolist end up in?)     {6}

 

 

 

 

 

b.   What test would have been needed in the if condition in Line 0?           {5}

 

 

 

 

 

 

 

c.   If Line 0 is the first test in the sorting algorithm, what additional test should be done after Line 3? {6}

 


11. Write an ANSI C program which reads in two parameters from the command line, a word and an int.  The program should take the input word and print it the input int number of times.  For example, the program called by >a.out lucky 3  should print out the word “lucky” (without quotes) three times. {6}

 

 

 

 

 

 

 

 

 

 

 

 

Extra Credit questions  - Worth two {2} points each unless stated otherwise.

 

XC1)   Which of the following is NOT a feature of a function?

 

A)        Can accept parameters

B)        Can return multiple values

C)        Can hold a variable value through multiple executions

D)        Can have local variable(s) which override global variable declarations

 

 

XC2)   Which of the following will NOT retrieve file input?

 

A)        fscanf            B)            getc

C)        fgets            D)            fwrite

 

 

XC3)   Which of the following is NOT related to error handling in C?

 

A)            filerror           B)            errno

C)        perror            D)            clearerr

 

 

XC4)   Which of the following is considered FIFO?

 

A)        stack            B)            queue

C)        linked list       D)            array

 

 

XC5)   Name one particular thing you learned from working on Lab 4 OR name your favorite captain with regard to question 10. {ANY answer will receive 2 (two) points}