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.      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]);  

}

 

 

 

 

 

______  ______  ______

 

 

______  ______  ______

 

 

______  ______  ______ 

 

 

 

  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);

 

}



 ______________  ______________  ______________

 

 

 

______________  ______________  ______________  ______________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  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");

}

 

 

 

__________________  __________________  __________________ 

 

 

 

 

 

  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

        | ________________ |

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

        |                  | 2040

        |_________________ |

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

        |                  |

 

 

 

  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] = _______________ ;



          ____________ = temp;
     }

}

 

void main(void)

{

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

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

    

     swap(_____________ , _________________);

    

     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.


_____________________________________________________________________

 

 

         

  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.


_____________________________________________________________________


  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;

 

___________ = fopen(______________,"r");

 

 

___________ = fopen(______________,"w");

 

 

while (EOF != fscanf(______________, _______________, &i))

 

 

fprintf(_____________,____________________, i);

 

 

fclose(_____________);

 

 

fclose(_____________);

}

 

 

  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(_____________);

     printf(")");

}

     }

 

void main (void)

{

     recprint(_____________);

     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(_____________,_____________, _____________, ______________);

 

 

 

 

 

 

 

  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 = __________________________________________;

 

 

 

        float rvalue = __________________________________________;

 

 

           

        if ( lvalue < ______________________________ )

 

 

            return ______ ;

 

 

        else if ( ________________________________________)

 

 

                  return ______;

 

            else

 

 

                  return ______;

           

 

     } /* 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.


_________________________________
/* assign 1001 to the cdno field of the variable named mycd  */


_________________________________________   
/* assign "One Heart" to the title field  */


_____________________________________
/* 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