Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: 12/10/10

 


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 14 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. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.

 

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, book, notes or other references during this examination.

DO NOT WRITE IN THIS SPACE

Section

Possible Score

Deduction

Grader

1

10

 

 

2

12

 

 

3

16

 

 

4

18

 

 

5

12

 

 

6

16

 

 

7

10

 

 

8

12

 

 

9

16

 

 

10

14

 

 

11

14

 

 

12

12

 

 

13

14

 

 

14

18

 

 

15

12

 

 

16

15

 

 

17

14

 

 

18

15

 

 

Total

250

 

 

 

 

Exam Scoreà

 

A

 

 

1.      Given the C program below, circle the count of how many distinct variables exist in the program.  There is only one correct answer.

 

#include <stdio.h>

 

int x = 2;

 

void myfunc(int x, int z)

{

  printf("%i ", x + z);

}

 

void myfunc2(int y)

{

  printf("%i ", x + y);

}

 

void main(void)

{

  int x = 1;

  int y;

 

  if(x == 1)

  {

    y = 3;

    myfunc(x, y);

  }

  else

    myfunc(y+x, y);

  myfunc2(x);

}

 

 

 

a)                  3

 

b)                  4

 

c)                   5

 

d)                   6

 

 

 

 

2.      Assume that the code in the previous problem compiles and runs without errors. Write the output of the program.

 

_________________      _________________________

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

     

int x = 5;

int y = 20;

int *ptra = &x;

int *ptrb = &y;

int *ptrc = ptra;

x = *ptrb / *ptrc;

 

When run, the operating system stores the variable “x” and “y” at memory address 2000 and 2004 respectively. Fill in the blanks with the correct values after the code above executes.

 

x = __________________________

y = __________________________

 *ptra = __________________________

 *ptrb = __________________________

 *ptrc = __________________________

  ptra = __________________________

  ptrb = __________________________

  ptrc = __________________________

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

void func(int * x, int y, int z )

{

     int i;

 

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

          x[i] = x[i] + (i+1)*y;

}

 

 

void main(void)

{

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

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

int * c;

 

 

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

 

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

 

 

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

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

 

     c = &a[1];

    

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

    

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

}

 

 

 

______________  ______________  ______________

 

______________  ______________  ______________

 

 

______________  ______________  ______________

 

 

 

 

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

 

#include <stdio.h>

 

int x;

int y;

 

void swap(int a , int b)

{

  int temp = b;

 

  y = a;

  x = temp;

}

 

void main(void)

{

  x = 4;

  y = 5;

 

  swap(x,y);

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

}

 

 

 

______________ ______________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Use the following data type to answer the next two questions.

 

typedef struct

{

  int id;

  char breed[32];

  char name[32];

  float weight;

} dogData;

 

 

6.      Fill in the blanks below that complete the printf statements so that the code has the following output:

 ID: 9678

 Breed: Rottweiler

 Name: fluffy

 Weight: 104.70

 

 

 

/*code start*/

#include <stdio.h>

 

typedef struct

{

  int id;

  char breed[32];

  char name[32];

  float weight;

} dogData;

 

 

void print_info(dogData dog, char name[], float * wtPtr)

{
  /*
Your answer must include the use of the variable named  dog  for the following two blanks*/

  printf("ID: %i \nBreed: %s\n",_______________, _______________);



  printf("Name: %s \nWeight: %.2f\n", ______________,______________);

}

 

void main(void)

{

  dogData dog = {9678,"Rottweiler","", 0.0};

  char * name = "fluffy";

  float wt  = 104.7;

 

  print_info(dog, name, &wt); 

}

 

 

7.      Given the user defined data-type called dogData above, assume you have an array named dogs of type dogData with 40 elements.

 

     dogData dogs[40];

 

Circle the code below that correctly assigns the name field of the 7 th element of dogs array to the breed field in the 14th element of dogs and leave other fields unmodified.

There is only on correct answer.

 

a)      dogs[14]=dogs[7];

 

 

b)      strcpy(dogs[13].breed,dogs[6].name);

 

 

c)      dogs[13]=dogs[6].breed;

 

 

 

d)      strcpy(dogs[6].breed,dogs[13].name);

 

 

8.      Which of the following C statements will dynamically allocate an array named a, that will hold 100 elements of data type float? There may be more than one correct answer. Circle each correct answer.

  

a) float * a = calloc(100*sizeof(float));

 

 

 

b) float * a = calloc(100,sizeof(float));

 

 

 

c) float * a = calloc(sizeof(float),100);

 

 

 

d) float * a = malloc(100*sizeof(float));

 

 

 

e) float * a = malloc(100,sizeof(float));

  

 

 

f) float * a = malloc(sizeof(float),100);

9.      Given the four C statements below, fill in each of the blanks with the roman numeral that describes the correct order of execution to read one integer value into a variable x from a file named data.txt .

 

           I.     fscanf(f, "%i", &x);

         II.     fclose(f);

       III.     FILE *f;

         IV.     f = fopen("data.txt", "r");

 

 

#include <stdio.h>

 

void main(void)

{

  int x;

 

  ____________________  /* fill in I or II  or III or IV */

 

 

  ____________________  /* fill in I or II  or III or IV */

 

 

  ____________________  /* fill in I or II  or III or IV */

 

 

  ____________________  /* fill in I or II  or III or IV */

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

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The next three problems refer to the following structure and array: 

typedef struct
{
  int  ssn;
  char name[32];
  int  age;
} Student;


Student students[550];

10.  Complete the C code for a comparison function named compareAgeAsc by filling in the blanks below.  When called by qsort the compareAgeAsc function should have qsort sort the students array in ascending order by the value in the age field.

 

int  compareAgeAsc(Student * l, Student * r)

{

                return    ________________________________________________;

 

 

11.   Complete the C code for a comparison function named compareNameDesc by filling in the blanks below. When called by qsort the compareNameDesc function should have qsort sort the students array in descending order by the value in the name field.

 

int  compareNameDesc(Student * l, Student * r)

{

                return    ________________________________________________;

}

 

 

12.  Fill in the blanks in the call to qsort to sort all 550 elements of the array named students based on compareAgeAsc.

 qsort (_____________,____________ ,_______________ ,_______________);

 

13.  Given the following structures and prototype for the function displayOneTrack (as in mp2), complete the code for the function named displayAlbumTracks by filling in the blanks.

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[MONTHS];  /* cd sales ($US) over the 12 months in 2005 */

        Track   tracks[20]; /* info for each track */

} Album;

/* prototype for displayOneTrack */

void displayOneTrack(Track * track)

This function first prompts to read a CDNO number from user. The message should be "Enter CDNO: ". Then it finds the album containing the given cdno number (using findCDNO). If the album exists, it first displays the album information (using displayOneAlbum) and then uses a loop to display the tracks on screen, one line one track (calling displayOneTrack to display one track).  If there is no album with the given cdno number (i.e., findCDNO returns -1), you need to print an error message "CDNO not found\n" and then return.

void displayAlbumTracks( Album albums[], int num_albums )
{
  int i, cdno, index;
 
  printf("Enter CDNO: ");
  scanf("%i", &cdno);
  index = findCDNO(albums, num_albums, cdno);


  if (______________________________)
        {
              printf("CDNO not found\n");

              ___________________________________________
        }
 displayOneAlbum(&albums[index]);



  for(i = 0; _____________________________ ; _____________________________)


        _______________________________________________________

}

 

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

 

#include <stdio.h>

#include <string.h>

typedef struct

{
  int   id;
  char  name[32];
  float percentage;
} Student;

 

 

 

void check_student(Student * ptr)

{

   Student student;

   student.id = 2;

   student.percentage = 100.00;

   if(strstr( ptr->name, "Ang") != NULL)

      strcpy( (*ptr).name, "Brad");

  

}

 

 

void main(void) 

{

    Student student = {1, "Angelina", 90.5};

check_student(&student);


     printf(" Id is: %i \n", student.id);
     printf(" Name is: %s \n", student.name);
     printf(" Percentage is: %.2f \n", student.percentage);
}

 

 

 

________________________________________________

 

 

________________________________________________

 

 

________________________________________________

 

 

 

 

 

 

15.  We will need to open a file using the “fopen” function under 3 scenarios, fill in the blank with the correct mode for opening the file, that is "r", "w" or "a".

 

 

I.      Concatenate characters to the end of file       mode = ___________

          

II.     Remove previous characters then add characters  mode = ___________  

III.   Read an integer from the file                   mode = ___________   

 

 

 

 

 

 

 

 

 

 

16.  The following C program compiles and runs without errors. Write the output produced by the program.

#include <stdio.h>

 

int fool(int n)

{

  if (n <= 1)

     return n;

  else

     return fool(n-1) + fool(n-2);

 }

 

 void main(void)

 {

     printf("%i %i %i \n", fool(2), fool(4), fool(5));

 }

 

 

 

__________    __________    ___________

 

 

 

 

 

 

 

 

 

17.  The following C program compiles and runs without errors. Write the output produced by the program.


    #include <stdio.h>
    #include <stdlib.h>
   
    int recFunc (int array[], int left, int right , int value)
    {
            int midpt = (left+right)/2;  /* integer division */

        if (value == array[midpt])  /* Voom! */
                return midpt;
        else if (value < array[midpt])
                return recFunc(array, left, midpt-1, value);
        else
             return recFunc(array, midpt+1, right, value);
    }

    int compare( int * ptr1 , int * ptr2)
    {
            return *ptr1 - *ptr2;
    }

    void main (void)

    {
        int data[] = { 7, 6, 3, 8, 5, 9};
        qsort(data, 6, sizeof(int), compare);

        printf("%i ", recFunc(data,0,5,6)  );
        printf("%i \n", recFunc(data,0,5,8)  );
    }

 

 

 

 

__________    __________

 

 

 

 

 

 

 

 

18.  In the movie "Inception", the main character Cobb dreams in his dream in his dream in his dream… This resembles ‘recursion’. The following C program compiles and runs without errors. Write the output produced by the program.

 

#include <stdio.h>

     void dream(int n)

{

  if (n == 1)

  {

     printf("love ");

     return;

  }

 

  if (n > 1)

  {

     printf("work ");

     dream(n-1);

printf("play ");

  }

}

 

void main(void)

{

     dream(3);

}

 

 

 

_________________________________________________________________