Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: December 16, 2008

 


No questions will be answered during this examination.  If you do not understand a question, read it again.  If you still do not understand it, make reasonable assumptions and write them down. (Points will be deducted if unreasonable assumptions are made.)

DO NOT CHEAT: Cheating includes not only copying from another person but also allowing someone to copy from you.  Anyone who copies or allows someone to copy will receive a score of zero.  So be defensive and protect your work.

This examination contains 13 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages.

Do not forget to sign the attendance list. If your exam is misplaced and you did not sign the attendance list then you will receive a zero score for the exam.

You may not use any electronic devices, books, notes or other references during this examination.

 

 

DO NOT WRITE IN THIS SPACE

Question

Possible Score

Deduction

Grader

1

16

 

 

2

9

 

 

3

14

 

 

4

15

 

 

5

16

 

 

6

15

 

 

7

10

 

 

8

14

 

 

9

12

 

 

10

8

 

 

11

20

 

 

12

15

 

 

13

16

 

 

14

16

 

 

15

17

 

 

16

19

 

 

17

18

 

 

Total

250

 

 

 

 

Exam Scoreà

 

 

A

 

 

 

 

1.      The following C program compiles and runs without errors. Write the output of this program.

 

 

#include <stdio.h>

 

 

void mystery(int a[], int b)

{

 

     int i;

     for(i = 0 ; i < b ; ++i)

          a[i] = a[i] + 1;

    

     b = 4;

}

 

 

 

void main(void)

{

 

     int a[3] = {1, 2, 3};

 

     int b = 3;

 

     mystery(a, b);

 

     printf("%i %i %i %i \n", a[0], a[1], a[2], b); 

 

}

 

 

 

 

           

 

 

 

___2___  ___3___  ___4___  __3____

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.      The following C program compiles and runs without errors. Write the output of this program.

 

#include <stdio.h>

void func(int * ptr, int length, int x )

{

     int i;

     for (i = 0; i < length; i++)

     {

          ptr[i] = ptr[i] - x;

     }

}

 

void main(void)

{

     int a[3] = {2, 4, 6};

int b[3] = {2, 4, 6};

int c[3] = {2, 4, 6};

     func(&a[0], 3, a[0]);

     printf("%i %i %i \n", a[0], a[1], a[2]);  

 

func(&b[1], 2, b[1]);

    
printf("%i %i %i \n", b[0], b[1], b[2]);  

 

     func(&c[2], 1, c[2]);

    

printf("%i %i %i \n", c[0], c[1], c[2]);  

}

 

 

 

 

 

___0___  ___2___  __4____

 

 

___2___  ___0___  ___2___

 

 

___2___  ___4___  ___0___ 

 

 

 

  1. The program below compiles and runs without errors. Write the output of this program.

 

#include <stdio.h>

 

void fun(int *a, int b, int *c, int *d)

{

     *c = (*a)* b;

     *a = 5;

     *d = 6;

 

     printf("%i %i %i \n", *a, *c, *d);

 

}

 

void main(void)

{

     int a, b, c;

     int *d = &b;

 

     a = 1;

     b = 2;

     c = 3;

    

 

     fun(&a, b, &c, d);

 

     printf("%i %i %i %i \n", a, b, c, *d);

 

}



 _____5_________  _______2_______  ______6________

 

 

 

______5________  _______6_______  ______2________  ______6________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1.   The program below compiles and runs without errors. Write the output of this program.

 

#include <stdio.h>

#include <string.h>

 

void main(void)

{

 

     int a, b;

     char c, d;

     a = 1;

     b = 2;

     char *ptr1 = "cs101";

     char *ptr2 = "dog";

 

     c = *(ptr1 + a);

     d = ptr2[b - 2];

 

     printf("%c ", c);

     printf("%c ", d);

     if (strstr(ptr1, ptr2) != NULL)

          printf("A");

     else

          printf("B");

}

 

 

 

_______s___________  ________d__________  _______B___________ 

 

 

 

 

 

  1. Assume the following two lines of code appear in a C program.

 

int a = 243;

int *b = &a;

 

When run, the operating system stores a at memory address 2036, and it stores b at memory address

2040.  Fill in the following diagram with the values stored in memory at the two addresses.

 

 

 

        |                  |

        --------------------

        |                  | 2036

        | _____243________ |

        --------------------

        |                  | 2040

        |______2036_______ |

        --------------------

        |                  |

 

 

 

  1. Complete the code in the C program below by filling in the blanks.  The program should swap the values in the two arrays and display the results shown below.

 

 

#include <stdio.h>

 

void swap( int *c , int *d)

{

     int i, temp;

     for(i = 0; i < 3; ++i)

     {

          temp = _____c[i]__________ ;



          c[i] = _____d[i]__________ ;



          ____d[i]________ = temp;
     }

}

 

void main(void)

{

     int a[]={1,2,3};

     int b[]={4,5,6};

    

     swap(______a_______ , ______b___________);

    

     printf(" %i %i %i \n", a[0],a[1],a[2]);

     printf(" %i %i %i \n", b[0],b[1],b[2]);

}

 

When the program is compiled and run it should display:

 

            4          5          6

            1          2          3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The next three questions refer to the following type definitions and variable declarations: 

 

typedef struct
{

          int  a;

          int  b;

} S;

 

typedef struct

{

          int  c;

          S    d;

          S   *e;

} T;

         

S  s = {1,2};

T  t = {3,{4,5}, &s};

S *ps = &s;

T *pt = &t;

    

         

  1. Fill in the blank below with a single C statement that changes the value of the b field of s to 5.


_______s.b = 5;_______________________

 

 

         

  1. Fill in the blank below with a single C statement that changes the value of the b field (of the d field) of t to 6.


__________t.d.b = 6;______________________________________________


  1. Circle each statement below that would NOT produce a compile error (in the context of the above definitions and declarations). There may be more than one correct answer.

 

a)   pt -> c = (&s) -> a;



 

b)   (*ps).b = (pt -> e).b;





c)   ps = t.e;




  1. Which of the following C statements will create an array named treated of size 100 of data type char?  (there is only one correct answer):

a) char * treated = calloc(100*strlen(treated));



b) char * treated = calloc(100*sizeof(char));



c) char * treated = calloc(100*sizeof(treated));



d) char * treated = calloc(100,sizeof(char));

 

 

 

 

  1. Fill in the blanks to complete the following program. This program will read a file named input.dat containing integers and write the integers to a file named output.dat.

 

 

#include <stdio.h>

 

 

void main(void)

{

char filename_in[] = "input.dat";

char filename_out[] = "output.dat";

char format[] = "%i";

FILE *fpin, *fpout;

int i;

 

___fpin________ = fopen(___filename_in___________,"r");

 

 

___fpout________ = fopen(__filename_out____________,"w");

 

 

while (EOF != fscanf(_fpin__, _"%i"__, &i))

 

 

fprintf(__fpout_,_____"%i "_____, i);

 

 

fclose(___fpin__________);

 

 

fclose(____fpout_________);

}

 

 

  1. Fill in the blanks to complete the following C program so that it compiles and runs without errors, and outputs:

     ((((*))))

 

 

 

 

 

#include <stdio.h>

 

 

void recprint (int num)

{

if (num == 0)     /* Voom!!! */

     printf("*");

else

{

     printf(______"("________);

 

     recprint(____num-1_________);

     printf(")");

}

     }

 

void main (void)

{

     recprint(_____4________);

     printf("\n");

}

 

 

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Circle each C program below that prints the integer value 10 to the screen when run. There may be more than one correct answer.

a)      #include <stdio.h>
 

int harpo (int n)

{

if (n <= 0)

      return 4;

else

return ( n-1 + harpo(n) );

            }

 

void main(void)
{

                  printf("%i \n", harpo(4));

            }



b)      #include <stdio.h>

int chico (int n)

{

if (n == 0)

return 0;

                  else

return ( n + chico(n-1) );

}

void main(void)
{

                  printf("%i \n", chico(4));

}

 

c)      #include <stdio.h>

int groucho (int n)

{

return ( 10 + groucho(n/5) );

}

 

void main(void)
{

                  printf("%i \n", groucho(4));

}

 

d)      #include <stdio.h>

int zeppo(int n)

{
      if (n % 2 == 1)

            return 10;

      else

            return ( zeppo(n/2) );

}

void main(void)
{

                  printf("%i \n", zeppo(4));

}

Use the following C code for the next three questions:

typedef struct
{
       char    name[40];   /* track name or song name */
       int     length;     /* in seconds */
}Track;

typedef struct
{
       int     cdno;       /* cd number */
       char    title[30];
       char    artist[20];
       int     year;
       int     num_tracks; /* number of tracks */
       int     quantity;
       float   price;
       float   sales[12];  /* cd sales ($US) over 12 months */
       Track   tracks[20]; /* info for each track */
} Album;

 

      Album albums[100];

      Album mycd;

 

 

 

 

 

 

  1. Fill in the blanks to have the qsort function sort the albums array in the order defined by the compareValueDesc function. You may assume that the albums array has been filled with 100 albums.

 /* qsort(arrayname , num_elts ,size_of_elts , compare_function) */

 

 

 

qsort(__albums_,__100_, _sizeof(Album)_, _compareValueDesc_);

 

 

 

 

 

 

 

  1. Complete the C code for a comparison function named compareValueDesc by filling in the blanks below.  When called by qsort the compareValueDesc function should have qsort sort the albums array in descending order by value in stock. The "value in stock" of an album is defined as the price of the album multiplied by the number of albums in stock. Both the price and quantity fields of the Album structure have to be used. 

int compareValueDesc( Album * l , Album * r )

     {

        float lvalue = ___l->price * l->quantity___;

 

 

 

        float rvalue = ____r->price * r->quantity__;

 

 

           

        if ( lvalue < ____rvalue__________________________ )

 

 

            return ___1___ ;

 

 

        else if ( _____lvalue > rvalue_____________)

 

 

                  return __-1____;

 

            else

 

 

                  return __0____;

           

 

     } /* end of compareValueDesc */

 

 

  1. Given the structures and the variable named mycd declared above (before question 14), complete the C code below by filling in the blanks to assign to the variable mycd the following values:
    cdno = 1001 , title = "One Heart",  sales for January = 1234.56 . Hint: use strcpy to assign strings.
    Fill in each blank with a single C statement that performs the specific assignment.


____
mycd.cdno = 1001; _____ /* assign 1001 to the cdno field of the variable named mycd  */


_____
strcpy(mycd.title, "One Heart");___  /* assign "One Heart" to the title field  */


_____
mycd.sales[0] = 1234.56; __* assign 1234.56 to the first element of the sales array */

  1. The output of the following program should be:
    y coordinate = 2.00

    Circle each correct choice listed below, that (by filling in the blank) would produce the output of 2.00 (as shown above) when the program is compiled and run.  There may be more than one correct answer.

    #include <stdio.h>

typedef struct
{

  double x;

  double y;

}cartesian;


void main(void)
{

     cartesian p1 = {1.00, 2.00};

     cartesian * p2 = &p1;

     cartesian * p3 = p2;

     printf("y coordinate = %4.2lf \n", _________________);

}



a)         p1

 

 

 

 

b)         y

 

 

 

c)        *p2.y

 

 

 

d)       p2->y

 

 

 

e)       p3.y

 

 

 

 

f)         (*p3).y