Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date:  5/14/09

 


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

16

 

 

3

12

 

 

4

16

 

 

5

16

 

 

6

20

 

 

7

10

 

 

8

14

 

 

9

14

 

 

10

13

 

 

11

12

 

 

12

24

 

 

13

20

 

 

14

16

 

 

15

15

 

 

16

16

 

 

Total

250

 

 

A

 

Exam Scoreà

 

 

 

 

 

 

 

 

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

 

 

#include <stdio.h>

 

void swap(int, int);

 

void main(void)

{

int x = 1;

int y = 2;

 

swap(x,y);

 

printf("x = %i , y = %i \n",x,y);

}

 

void swap(int y , int x)

{

return;

}   

 

 

            x = ____________________     y = ____________________    

 

 

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

 

#include <stdio.h>

int j = 5;

int i = 6;

 

void func(void);

 

void main(void)

{

int j = 2;

 

func();

printf("i = %i j = %i\n",i ,j);

}

 

void func(void)

{
          int i = 7;

j = 1985;

 

return;

}

 

 

            i = ____________________     j = ____________________    

3.      The following program compiles and runs without errors.  Write the output of the program.

 #include <stdio.h>

void main(void)

{

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

int i;

int *s1;

int *s2;

int *s3;

int a,b,c,d;

 

s1 = x;

s3 = &s1[3];

s2 = &s3[1];

a = s1[1];

b = s1[2];

c = s2[0];

d = s3[0];

 

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

}

 

 

            ___________    ___________    ___________    ___________     

 

 

 

4.      The following program compiles and runs without errors.  Write the output of the program.

  #include <stdio.h>

  #include <string.h>

   typedef struct{

     char name[32];

     int  age;

   } animal;
  
  void main(void)
  {    
     animal pet = {"dog",3};
     char * ptrA = "cat";
     char * ptrB; 

 

     printf(" %s ", pet.name);
     printf(" %c ", pet.name[1]);
     strcpy(pet.name, ptrA); 
     ptrB = pet.name;
     printf(" %c ", ptrB[0]);

     printf(" %c ",*ptrB); 

  }
 

         ______________    ______________    ______________    ______________        

5.      The following program compiles and runs without errors.  Write the output of the program.

 

#include <stdio.h>

 

void swap3(int* x, int* y)

{

int temp = *x;

 

*x = *y;

*y = temp;
}

 

void swap2(int* x)

{

int temp = x[0];

 

x[0] = x[1];

x[1] = x[2];

x[2] = temp;

}

 

void swap1(int x, int y)

{

int temp = x;

 

x = y;

y = temp;

}

 

void main(void)

{

int a[3]={11, 13, 17};

int* b;

 

swap1(a[0], a[1]);

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

swap2(a);

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

b = &a[1];

swap3(&b[0], &b[1]);

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

}

 

 

            ________________     ________________     ________________  

 

 

             ________________     ________________     ________________  

 

 

            ________________     ________________   

 

 

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

      int a = 7;

int b = 49;

int *ptra = &a;

int *ptrb = &b;

int ans;

ans = *ptrb / *ptra;

 

When run, the operating system stores the variable “a” at memory address 2036, and the remaining variables are located at the addresses shown in the figure below.

Fill in the following diagram with the values stored in memory after the code above executes.

 

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

   a    |                  | 2036

        | ________________ |

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

   b    |                  | 2040

        |_________________ |

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

 ptra   |                  | 2044

        |_________________ |

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

 ptrb   |                  | 2048

        | ________________ |

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

  ans   |                  | 2052

        |_________________ |

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

                         

 

 

 

 

 

7.      Which of the following C statements will dynamically allocate an array named arr that will be

able to hold 100 elements of data type double? There may be more than one correct answer. Circle

each correct answer.

 

 

a) double * arr = calloc(100,sizeof(double));

 

 

 

b) double arr = malloc(100*sizeof(double));

 

 

 

c) double * arr = calloc(sizeof(double),100);

 

 

 

d) double * arr = malloc(100*sizeof(double));

 

 

 

e) double arr = calloc(100,sizeof(double));

 

8.      Fill in the blanks to sort the three-element array named a according to the order defined by the

function named comp. Next, write the output this program produces.

 

#include <stdio.h>

#include <stdlib.h>

typedef struct

{

int numerator;

     int denominator;

} Fraction;

 

int comp(Fraction * s, Fraction * t)

{

return ((s -> numerator) % (s -> denominator)) -

            ((t -> numerator) % (t -> denominator)); 

}

 

void main(void)

{

     Fraction a[3];

int i;

a[0].numerator = 7;

a[0].denominator = 9;

a[1].numerator = 5;

a[1].denominator = 9;

a[2].numerator = 11;

a[2].denominator = 9; 

 

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

 

 

qsort(_______________,_________,______________,____________); 

 

 

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

printf("%i/%i \n",a[i].numerator, a[i].denominator);

}

 

 

 

            ____________________________________________________________________

 

 

 

 

            ____________________________________________________________________

 

 

 

 

            ____________________________________________________________________

 

9.      Assume we have the following type definition:

 

typedef struct {

     int x;  

     int y;    

} myStruct;

 

and the following variable declarations and initializations:

 

myStruct a;

myStruct * p = &a;

int x  = 0;

 

Which of the following is a correct statement for assigning the value 7 to the x field of the variable named a?   That is, which of the following choices does the same thing as the following line of code?

 

a.x = 7;

 

Please circle each correct answer. There may be more than one correct answer.

 

 

a) (*a).x = 7;

 

 

 

b) (&a).x = 7;

 

 

 

c) (*a) -> x = 7;

 

 

 

d) (*p) -> x = 7;

 

 

 

e) p -> x = 7;

 

 

 

f) p[x].x = 7;

 

 

 

g) (&a) -> x = 7;

 

 

 

 

 

 

10.  Consider the following data type definition:

 

typedef struct

{

     char type[25];

char jelly[25];

char bread[25];

int  layers;

} pbj;

 

 

Suppose we need to manage a number of peanut butter and jelly sandwiches for a party. We

aren't expecting more than 500 sandwiches. So, we'll declare an array of 500 sandwiches:

pbj sandwiches[500];

 

a)      Write a single line of C code to initialize the first element of the sandwiches array so that the

peanut butter type is “smooth”.

.

            ____________________________________________________________________

 

b)      Write a single line of C code to initialize the first element of the sandwiches array so that the

 number of layers equals 2.

 

            ____________________________________________________________________

 

c)      Write a single line of C code to assign all the fields (members) of the first element of the sandwiches array to the 500th element of the sandwiches array.

 

            ____________________________________________________________________

 

 

 

 

11.  Write C code to create a structure data type named Student, having two fields (members)

of integer data type called id and credithours.

 

 


________________________________________



________________________________________



________________________________________



________________________________________

12.  The information (name and age) of NBA players is stored in the file “player.dat”, as follows:

 

LeBron James; 24

Eddie House; 30

Kobe Bryant; 30

 

Fill in the blanks to complete the following program, which should read contents from the file

player.dat” and write only the information of Kobe Bryant to the file named “kobe.dat”.

Note that you cannot use “%s” to read the player name since the names contain spaces.

 

 

#include <stdio.h>

#include <string.h>

 

void main(void)

{

  FILE *fin;

  FILE *fout;

  char name[50];

  int age;

 

  fin = fopen("player.dat", __________________);

 

  fout = fopen("kobe.dat", __________________);

 

 
  while(EOF
!= fscanf(________,_____________,__________,_________))

       {

 

    if(strcmp(_____________________,"Kobe Bryant") == 0)

 

 

fprintf(__________,"%s; %i",__________,__________);

 

  }

 

  /* close the input file */

  fclose(___________________);

 

  /* close the output file */


  fclose(___________________);

 

}

 

 

 

 

13.  The following program compiles and runs without errors. Write the output of the program.


#include <stdio.h>

typedef struct{
        int length;
        float weight;
} Fish;


void ptrChange(Fish *f)

{
        f->length = 15;
        f->weight = 4.9;
}

void nonPtrChange(Fish f)

{
        f.length = 16;
        f.weight = 6.4;
}

void main(void)

{
  Fish f1,f3;
  Fish *f2;

 
  f1.length = 14;
  f1.weight = 5.9;
  nonPtrChange(f1);
  printf("length = %i , weight = %f \n", f1.length, f1.weight);

  f3.length = 17;
  f3.weight = 9.9;

  f2 = &f3;
  ptrChange(f2);
  printf("length = %i , weight = %f \n", f2->length, f2->weight);
}


      length = ________________ , weight = ________________



      length = ________________ , weight = ________________

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

int func(int x, int y)

{

          if (x*x  == y) /* Voom! */

              return x;

     else if ( x*x < y)

              return func(x+1,y);

          else

              return func(x-1,y);

}

 

void main(void)

{

     printf("%i ", func(1,1));

     printf("%i ", func(2,9));

     printf("%i ", func(5,16));

     printf("%i ", func(1,64));

}

 

 

      _________________    _________________    _________________    _________________

 

 

 

 

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

        #include <stdio.h>


      int   mystery( int n)
      {
            if ( n <= 1)  /* Voom! */
                  return n;
            else
                  return  mystery(n - 3); 
      }


      void main(void)

      {
            printf(" %i  ", mystery(5));
            printf(" %i  ", mystery(6));
            printf(" %i  ", mystery(7));
      }


            ____________   ____________   ____________ 

 

 

16.  Complete the following program by filling in the blank on this page and the blanks on the next

page. The compare function should tell the qsort function to sort the array named tracks in

ascending order by the value in the name field. When the program is completed, compiled and

run it should display the following output,

 

 

Another Side of Bob Dylan

   All I Really Want To Do 400

   Black Crow Blues 320

 

Time Out of Mind

   Dirt Road Blues 500

   Love Sick 320

 

 

 

 

 

 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

 

typedef struct {

     char name[40];  

     int length;    

} Track;

 

 

typedef struct {

     char title[30];

     Track tracks[2];

} Album;

 

 

int compare(Track * a, Track * b)

{

 

  return _______________________________________________________ ;

 

}

 

 

 

 

 

     /* code continues on the next page */

 

 

 

 

 

 

void main(void)

{

Album albums[2];

int i,j;

 

strcpy(albums[0].title, "Another Side of Bob Dylan");

strcpy(albums[0].tracks[0].name, "Black Crow Blues");

albums[0].tracks[0].length = 320;

strcpy(albums[0].tracks[1].name, "All I Really Want To Do");

albums[0].tracks[1].length = 400;

 

strcpy(albums[1].title, "Time Out of Mind");

strcpy(albums[1].tracks[0].name, "Love Sick");

albums[1].tracks[0].length = 320;

strcpy(albums[1].tracks[1].name, "Dirt Road Blues");

albums[1].tracks[1].length = 500;

    
/* for each album sort the tracks in ascending order by names */

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

           
qsort(___________________,_______________,sizeof(Track),compare);

 

 

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

  {

printf("%s \n", albums[i].title);

for(j = 0; j < 2; ++j)

{

  printf("   %s ", albums[i].tracks[j].name);

       printf("%i \n", albums[i].tracks[j].length);

}

printf("\n");

            }

}