1.    Topics: functions, loops, logical expressions, arithmetic expressions


If  n = 12345, what is the value returned from fun?


int fun( int n )


   int d;

   int r;


   r = 0;

   while( n )


      d = n % 10;

      r = r * 10 + d;

      n = n / 10;


   return r;








2.    Topics: functions, loops, 1D arrays


(A)  Assume ary is declared as:


int ary[10] = {5, 3, 7, 6, 15, 8, 10};


     and size, the length of ary, is 7


What is the value returned from the function guess?


int guess( int ary[], int size )


   int j;

   int k;


   j = ary[size – 1];

   for( k = size – 2; k >= 0; k-- )

       if( j > ary[k] )

           j = ary[k];

   return j;





(B)  What does the function guess do ?








3.    Topic: pointers and arithmetic expressions


Given the following declarations:


   int x = 10;

   int y = 20;

   int z;

   int *px = &x;

   int *py = &y;


Which of the following expressions are valid?  If valid, give the value of z


a.   z = (*px)++;          


b. z = *py + y;


c. z = *x + *y;








4.     Topics: pointers,  1D arrays


Given the following declarations


  int *p;

  int A[5] = {1,2,3,4,5};


Write code that uses p to print the third element of A






5.     Topics: functions, 2D arrays


Given the following declaration


   int array[4][3];


(A)    Assume there are 2 functions: fA and fB


fA returns void, and accepts one row of a 2D array as input parameter.  Write the prototype for fA.


fB returns an integer, and accepts one element of a 2D array as input parameter.  Write the prototype for fB.




(B)    Assume the same fA and fB as above


Write code to call fA and pass the first row of array.


Write code to call fB and pass the first element of the third row of array.







6.     Topics: 2D arrays, strings


Given the following declaration


   char words [][8] = { {“I”}, {“want”}, {“CIS15BG”} };


(A)  Write code to change the string CIS15BG to CIS15C by overwriting the string.


(B)  Write code to change the string CIS15BG to CIS15C by overwriting the appropriate characters.


(C)  Write one printf statement that uses the array words to print the string  I want CIS15C






7.    Topics : pointers, strings


Given the following program fragment, show what is printed.


char str[] = "abcd";

char *pStr;


pStr = str;


printf("%s ", pStr );



printf("%s ", str );






8.    Topics: structures, strings


(A)  Define a structure STUDENT that contains:

1.  a social security number. The social security number is a string of 9 characters

2.  an array of 3 names: first, middle, last.  Each name can be up to 20 characters long

3.  a GPA, a floating point number



(B)  Declare a structure called studentA of type STUDENT



(C)  Assign the following values for studentA:

          123456789 for social security number

          “Ima College Student” for first, middle, and last names

          3.85 for GPA



(D)  Given that pStudent is a pointer to the student structure, write code so pStudent points to studentA



(E)  Give 2 solutions to print the last name of studentA








9.    Topic: linked lists


There are two mistakes in the following fragment of code. Find them and explain.

Assume pList points to the first node in a linked list with data nodes.


pWalk = pList;

while( pWalk )


         process( pWalk->data );



printNode( pWalk->data );








10.       Topic: linked lists

Assume list1 is a pointer that points to the first node of a linked list with 3 nodes,

and list2 is a pointer that points to the first node of a different linked list with 4 nodes.

Both lists have the same type of nodes.


Assume the following statements are applied to the two lists:


pNode = NULL;

pWalk = list1;

while( pWalk )


   pNode = pWalk;

   pWalk = pWalk->next;


temp = list2->next;

pNode->next = temp->next;

temp->next = list1;

list1 = NULL;



Draw a diagram of the resulting two lists.


list1 ->


list2 ->







11.       Topics: structures with pointer, nested structures


pHead is a pointer to a structure called HEADER. 

This structure contains two fields: a counter (type int) and a pointer to an array of structures, called pAry.

The base type of the pAry array is another structure called BOOK.

This BOOK structure contains three fields: title (a dynamically allocated string), author (30 characters),

and date of type DATE (a structure with three integers: month, day, and year).


Code a printf statement to print the title, the first letter of the author’s name,

and year of the third book in the array.





You are visitor number: 22694