Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                               Date: May 2, 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

12

 

 

2

12

 

 

3

12

 

 

4

10

 

 

5

15

 

 

6

18

 

 

7

12

 

 

8

20

 

 

9

16

 

 

10

20

 

 

11

14

 

 

12

12

 

 

13

15

 

 

14

12

 

 

15

16

 

 

16

16

 

 

17

18

 

 

Total

250

 

 

 

 

Exam Scoreà

 

 

A

 

 

 

 

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

 

#include <stdio.h>

 

int a = 0;

 

int f(int a)

{

a = a + 1;

     printf("%i ", a);

                        return a;

}

 

void main(void)

{

int a = 10;

                        int b;

b = f(a);

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

}

 

 

            ___________________________________________________________________

 

 

 

 

 

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

 

#include <stdio.h>

 

void replace(int a, int b, int *c)

{

a = b;

*c = a;

}

 

void main(void)

{

int a = 10;

int b = 9;

                        int c = 8;

 

replace( b,a, &c);

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

}

 

 

            ___________________________________________________________

3.      Complete the program below by filling in the blanks so that when your program is run it should
            produce the following output:

     9  10

 

 

#include <stdio.h>

 

void swap( _________________ , _________________ )

{

int temp = *a;

     *a = *b;

     __________________ = temp;

}

 

void main(void)

{

    int a = 10;

    int b = 9;

    swap(____________,____________);

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

}

 

 

 

 

 

4.      The following program contains one error. The program should print the following values:

     2008  2008


            Write the line number that contains the error and write the corrected line of code.
             (The line numbers are not part of the code.)

1.   #include <stdio.h>

2.   #include <stdlib.h>

3.   void main(void)

4.   {

 5.    int *x, *y;

 6.    x = malloc(sizeof(int));

 7.    *x = 2008;

 8.    y = *x;

    9.    printf("%i  %i \n", *x, *y);

 10. }

 

            Line number that contains an error: __________


            Corrected line of code: __________________________________________________________

 

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

 

#include <stdio.h>

void main(void)

{

int array[] = {2, 4, 6, 8};

int *x = &array[2];

*x = 3;

printf("%i %i %i", *x, array[2], x[0]);

}

 

 

            ___________________________________________________________________

 

 

 

6.      Given the following C code:

     typedef struct
     {
          int up;
          int down;
     } jump;

 

   jump manyJumpers[10];



  a) Write a single line of C code to assign the value 5 to the down member(field) of the second
       element of the array manyJumpers. Hint: array indexing starts with 0.

        
          ________________________________________________________________________


b) Write a single line of C code to assign the value of the  down member(field) in the third 
     element of the manyJumpers array to the down member(field) of tenth element of the
     manyJumpers array.
       

          ________________________________________________________________________


c) Write a single line of code to give the second element of the manyJumpers array the same
     values as the third element of manyJumpers array.


                        _______________________________________________________________________

7.      Fill in the blanks to complete the code for the function named calculatePerimeter that
            computes the perimeter of the given circle and assigns it to the perimeter field of the given   

      Circle structure.

The formula for the perimeter of a circle given its radius is:

                       

           

            For example, running the following program with your code for calculatePerimeter

#include <stdio.h>

#define PI 3.141592653589793

 

typedef struct {

        float radius;

        float perimeter;

} Circle;

 

void calculatePerimeter(Circle *c);


           
void main(void)

{

         Circle c;

         c.radius = 1.00;

         calculatePerimeter(&c);
         printf(" %.2f %.2f \n", c.radius, c.perimeter);

}

 

     should display the values   1.00   6.28 . However your code should work for any non-negative value  

            assigned to the radius.

 

 

/* Hint: the parameter c below is a pointer.  */

 

void calculatePerimeter(Circle *c)

{

 

       ____________________= 2.0 * PI * ____________________________;

}





 

 

 

8.      Fill in the blanks to complete the following program. The program should first read the names and
            ages of ten students into an array named students. Next the program calls the function
            named printStd to print the names and ages of these students. The function printStd should
            print the values of the name and age fields of the structure pointed to by its input parameter.


#include <stdio.h>

 

 typedef struct

{

    char name[255];

    int age;

} Student;



 

void printStd(Student * stdPtr)

{

    printf("%s ", ___________________________);



    printf("%i" , ___________________________);

}

 

void main(void)

{

    Student students[10];

                      int i;

 

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

        {

      scanf("%s", __________________________);

 

  scanf("%i", __________________________);
}

 

for(i=0; i < 10; ++i)
{
   printStd(_____________________);

   printf("\n");

}

      }


 

 

 

 

 

 

 

 

9.  Complete the compAgeDesc comparison function so that when it is called by qsort, it would
            cause qsort to sort an array of data type Student in descending order by the value in the age
    
field.

 

 

Do NOT write code for the main function. Using the printf or scanf functions or writing any

            other code besides the code for the compAgeDesc function will give you a zero score for this     

            problem.

 

 

You may assume the following structure is given

 

 

typedef struct

{
    char name[255];

    int age;

} Student;

 

 

int compAgeDesc(Student * a, Student * b)

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

10. Complete the code below to write your lab section to the file named "cs101.dat". If you forget your

       lab section then use section "AYA".

 

#include <stdio.h>

 

void main(void)

{

 

   char * section = _______________ ;

 

   FILE* fileptr = fopen(__________________, ___________________);



   fprintf(_________,____________, section);



   fclose(_______________________);

 

}

 

 

 

 

 

 

 

 

11. You are given the variable ptr declared below. Using either of the C functions calloc or 

      malloc, write a single C statement to allocate memory for an array with the name ptr that holds   

      101 values of data type double.

 

 

double * ptr;

 

 

____________________________________________________________________________

12.  Given the following function:

int f(int a, int b)

{

  if (a < 0)

return 0;

 

  return f(a - 1, b + 1);

}

 

which of the following statements would be true if f were called with two integer arguments in a correct program that contains the above definition of f?

 

 

 Circle each true statement (there may be more than one). 

 

a)      The function f is a recursive function.




b)      The execution of f will not terminate.




c)      The execution of f will terminate only if b is less than -1.




d)     The function f always returns the value 0. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

int mystery(int a, int b)

{

  if (a == 0)

     return -b;

  else if ( a > 0)

          return mystery(a - 1, b - 1);

       else

          return a - b;

}

 

void main(void)

{

  printf(" %i \n", mystery(0,3));

  printf(" %i \n", mystery(-1,2));
  printf(" %i \n", mystery(2,2));

  printf(" %i \n", mystery(3,2));
  printf(" %i \n", mystery(300,200));

 

}

 

 

            ________________________________________________________

 

 

            ________________________________________________________



________________________________________________________

 

 

________________________________________________________



________________________________________________________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.  Given the following C code:


typedef struct

{
     int     employeeID;      
     int     departmentID;
     char    fname[32];
     char    lname[32];
} Employee;

 

Employee emp = { 1001, 101, "John", "Smith"};

Employee * empPtr;

empPtr = &emp;

   printf("employeeID is %i in department %i \n",

           empPtr->employeeID, empPtr->departmentID);

 

 

Circle each of the following statements that produce results identical to the above printf
    
statement.

 

a) printf("employeeID is %i in department %i \n",

               *empPtr.employeeID, *empPtr.departmentID);

 

 



b) printf("employeeID is %i in department %i \n",                                  (*empPtr).employeeID, (*empPtr).departmentID);

 



 

c) printf("employeeID is %i in department %i \n",

               *(empPtr.employeeID), *(empPtr.departmentID));

 

 

 

 

d) printf("employeeID is %i in department %i \n",

                emp.employeeID, emp.departmentID);

 

 

 

 

 

 

 

 

 

 

15.  Given the C code:

typedef struct
{
  int month;
  char day[32];
  int year;
} date;


typedef struct
{
  int hour;
  int minute;
  int second;
} time;

typedef struct
{
  date  d;
  time  t;
} event;

 

event graduation; 

 Fill in the blanks below with C code to assign to the variable named graduation the date Sunday May 11, 2008 and time 10:30:00 am.

You are asked only to fill in the values for the
day, year, hour and minute fields.

You may use any C library function.

 




 

_________________________________________________    /* assign the day, "Sunday"*/


_________________________________________________    /* assign the year, 2008 */


_________________________________________________   /* assign the hour, 10 */


_________________________________________________   /* assign the minute, 30 */


Use the following C code for the next two questions:

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

      typedef struct 
      {
          int     cdno;     
          char    title[30];
          char    artist[20];
          int     year;
          int     num_tracks;
          int     quantity;
          float   price;
          Track   tracks[20];
      } Album;
 
      Album albums[100]; 
 

 

16.  Fill in the blanks to call qsort to sort the array named albums. You may assume that the array albums has been filled with 100 albums and that you have written a comparison function named compTitleAsc that qsort will call to sort the array in ascending order by title.

 

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


qsort( ___________ , ____________, _____________, ______________);

 

 

17.  Write a complete function named compTitleAsc . The function qsort will call compTitleAsc to sort the array in ascending order by the value in the title field.


Do NOT write code for the main function. Using the printf or scanf functions or writing any other code besides the code for the compTitleAsc function will give you a zero score for this problem.

 

int compTitleAsc( Album * a, Album * b)

{

 

 

 

 

 

 

 

 

 

 

}