Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam  (120 minutes)

 

Name:                                                                         NetID:
 

Lab Section:                                                               Date: August 4, 2006

 

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.

The number of points for a question is roughly proportional to the amount of time you may need for it. Don’t spend too much time on any one question.

Do not forget to sign the attendance list and to write your signature on the line below:

 

_______________________________________________________________________
(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

Score

Grader

1

20

 

 

2

10

 

 

3

20

 

 

4

12

 

 

5

28

 

 

6

16

 

 

7

20

 

 

8

25

 

 

9

12

 

 

10

20

 

 

11

16

 

 

12

20

 

 

13

20

 

 

14

20

 

 

15

20

 

 

16

35

 

 

17

21

 

 

Total

335

 

 

 

1.      You are in your Unix home directory. You have the following subdirectories:

 

  subdirectory:  files in it

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

    lab7          lab7.c , example1.c , example2.c  

    lab8          lab8.c , guess.c
    lab9          lab9.c , bandit.c       
            

 

 


a)
Assuming your pwd (current directory) is your home directory, write a single Unix command   

    to copy both example1.c and example2.c from the lab7 directory  into the lab8 directory.




       

     b) Assuming your pwd (current directory) is your home directory, write a single Unix

                 command to make the lab9 directory your current directory.

                 

     

            c) Assuming that
lab9 is your current directory, write a single Unix command to move the file

    guess.c (located in the lab8 directory) into the lab9 directory. Do not change the

    name of the file.

 

    

     

       

 

     d) Assuming that lab9 is your current directory, write a single Unix command to rename the file

    named guess.c (now located in the lab9 directory) as  newguess.c  .

               

 

 

 

 

   

 

 

2.      a) Write a single Unix command to compile the C program in the file named myprog.c  using the gcc compiler.





b) Using Unix redirection( <), write a single Unix command to run your program (compiled successfully in part a) above) and read input data from the file named input.dat  .




 

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

#include <stdio.h>

void mess(int * me, int you) {

     *me = *me * 2;

     you = you *3;
  printf("you = %i and me = %i\n", you, *me);

}

 

void main(void) {

  int you;

  int me;

  you = 5;

  me = 7;

  mess(&you,me); /* Note parameter order */

  printf("you = %i and me = %i\n", you, me);

}

 

 

Output:

 

you = __________ and me = ____________

 

you = __________ and me = ____________

 

 

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

Hint: look closely at this code.

 

#include <stdio.h>

 

void main(void) {

 

  int x = 2.5;

 

  if (x > 3) {

       printf("D ");

  }   

  else if (x == 2.5) {

       printf("E ");

  }

  else {

       printf("F ");

  }

 

}

 

 

Write the output here:


_______________________________________________________________

5.      Write a complete program that asks a user for a number between one and ten and if the number is between one and ten (including one and ten) prints “Good” but if it's outside that range it prints “Sorry”.  Don't forget any necessary include statements. Assume that the user only types integers as inputs.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.      Complete the code for the function rolldie below that generates a random integer between 1 and 6 inclusive:

 

#include <stdio.h>

#include <stdlib.h>

 

 

int rolldie(void){

 /* write your code here */

 /* make sure to declare any variables you use */

 

 

 

 

 

 

}

 

 

 

void main(void) {

 

  int r;

 

  r = rolldie();

       

  printf("Random number: %i\n", r);

 

}

7.      Complete the program to calculate and print the average of the elements in the array arr:

 

#include <stdio.h>

 

void main(void) {

int arr[10] = {5, 3, 1, 2, 4, 7, 6, 9, 8, 0};

double average;

 /* declare any variables you need here */

 

 

 

 /* sum the values of the array */

 

 

 

 

 

 

 

 /* compute the average */

 

 

 

 

 /* print the average */

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8.      Fill in the blanks to complete the program below.  This program reads the contents of the file input.dat into the array named studs of  data type Student .  The Student structure is defined this way:

 

typedef struct {

  char full_name[50];

  int ssn;

  float tuition_due;

} Student;

 

The file input.dat consists of one line per file, in the following format:

 

full_name / ssn / tuition_due

 

So, for example, the following is a line from the file:

 

John Smith / 123456789 / 1234.56

 

 

 

void main(void) {

 

  Student studs[10];

 

  char filename[] = “input.dat”;

 

  FILE *fileIn;

 

  int count = 0;

 

   fileIn = fopen(______________________  ,  ________________);
   if (fileIn == NULL){
        printf(“file not found \n”);
        return;

   }
      

  while(EOF != fscanf(_________________,"__________________________",

 

       ____________________________________________________________)) {

 

       count++;

  }

 

 

   /* other code not show here */

}

 

 

 

 

 

 

9.         Write the output of the following program:

 

#include <stdio.h>

 

void main(void) {

 

  int a = 1;

 

  switch(a) {

  case 1:

       printf("1 ");

  case 2:

       printf("2 ");

  case 3:

       printf("3 ");

  default:

       printf("d ");

  }

 

}

 

Output:

 

_________________________________________________________

 

 

10.  Write the output when the following program executes.

   int plus_one(int x) {
         x = x + 1;
         printf("%i ", x);
         return x;
   }

  void plus_two(int x) {
         x = x + 2;
         printf("%i ", x);
         return ;
   }
  
   void main(void) {
        int x = 1;
        x = plus_one(x);
        printf("%i ", x);
        plus_two(x);
        printf("%i ", x);
  }

 

 

Output: __________________________________________________________

11.  Given the following code fragment in C:

 

int x[3] = {5, 6, 7};

 

int * p = &x[0];

 

int y = p[0];

 


Which of the following statements produce the same output as the following statement:

 

 

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

 

 

Circle all that apply:

 

 

a) printf("%i", p[0]);

 

 

 

 

b) printf("%i", y);

 

 

 

 

c) printf("%i",*p);

 

 

 

 

d) printf("%i",*x);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

12.  Which of the following are valid prototypes for this function:

 

void myFun(int x, float y) {

 /* code goes here... */

}

 

Circle all correct prototypes. (Do not write any code.)

 

 

a) void myFun(int x, float y);


 

 

b) void myFun(int,float);

 




c) myFun(int x, float y);





d) int myFun(int x, float y);





e) int myFun(float y, int x);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

13.  Given the declarations below complete the C code by filling in the blanks with the correct answers.

#include <stdio.h>
 
typedef struct {
  double real;
  double imaginary;
} complex;

complex sum(complex,complex); /* prototype */

void main(void){
  complex x = {1.0, 2.0};
  complex y = {1.0, 2.0};
  complex z;

  z = sum(x,y);  /* perform "complex" sum of x and y and assign to z */

  /* print the real and imaginary values(fields) of z */

  printf(" real(z) = %lf ",  _____________________________);
 
  printf(" imag(z) = %lf \n",_____________________________);
}

/*
 to add two complex numbers use the rule {real1,imaginary1} +      
    {real2,imaginary2} = {real1 + real2, imaginary1 + imaginary2} 
 so {1.0,2.0} + {1.0,2.0} = {2.0,4.0} 
*/

  complex   sum(complex a , complex b){
   /* declare c and initialize */
   /* c is initialized with the sum of a and b */
   complex c = {_____________________,________________________________};
        return  c;
  }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

14.  Assume the data type complex has been defined as in the previous question:

typedef struct {
  double real;
  double imaginary;
} complex;


Further assume
p is a pointer to a complex object and that variables named value and z have been declared and initialized as follows:

 

complex * p;

double value;

complex z = {3.0 , 4.0 };

p = &z;

 

Circle the valid statements (statement that do not produce compiler errors):

 

a) value = p.real;

 

 

 

b)  value = p->real;

 

 

 

c)  value = (*p).real;

 

 

 

d) value = (*p)->real;

 

 

 

e) value = p[0].real;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

15.  Write the output the following program produces.

#include <stdio.h>
#include <string.h>

void main(void)
{
   char password[32] = "CS101";
   printf("%s \n", password);
   printf("%c \n",password[1]);
   strcpy(password, "CS105");
   printf("%s \n", password);

   if (strcmp("CS105
", "CS105")  > 0)
      printf("yes \n");
   else
      printf("no \n");
 
   if (strstr("CS105", "CS") != NULL)
      printf("good \n");
   else
      printf("bad \n");
}
 
Output:
 

_________________________

 
 

_________________________

 
 
_________________________
 
 

_________________________

 

 

_________________________

 

 

 

 

 

 
 
 
 
 
 
 
 

16.           Fill in the blanks and write code for a comparison function named compare_weights  that compares two atomic weights.  This comparison function should tell qsort to sort elements in the array named AE in descending order by weight. Next complete the code in main (by filling in the blanks) that will sort the elements in descending order and print out the elements. 

#include  <stdio.h>
#include  <stdlib.h>

typedef struct {
  char name[15];
  char symbol[3];
  int number;
  double weight;
} Element;

/* fill in the blanks */

int compare_weights( ___________________ , _____________________) {

 /* write your code here */

 

 

 

 


}

void main(void){
  int i;
  Element AE[4] ={{"Hydrogen", "H", 1, 1.00794},
                  { "Beryllium", "Be", 4, 9.01218},
                  {"Gold", "Au", 79, 196.96655},
                  {"Carbon" ,"C" ,6 ,12.0107}   }; 
  /* fill in the blanks sort the elements in descending order */
   

  qsort( _________________________ ,____________________________ ,

        __________________________ ,_________________________ );

  for(i=0; i < 4;++i)           
    printf("%s %s %i %lf\n", AE[i].name, AE[i].symbol, AE[i].number,
            AE[i].weight);
}

17.  Complete the following program by filling in the blanks to write a program that will dynamically allocate memory (DMA) for an array of integers of sufficient size to read all the numbers a user will enter at the keyboard. The program will print the smallest value typed by the user. The first number the user types will be the size of the array. This number is not to be stored in the array. Your code should work correctly for any values of integers the user enters.

 

#include <stdio.h>
#include <stdlib.h>

 

void main(void) {

  int i, min;

  int size; /* size of array */

int * ptr; /* name of array */

    

  /* prompt user to enter the size of the array */

  printf("Enter array size: ");

  scanf("%i",&size);     

    

  /* use DMA to allocate memory for array of length = size */


  ptr  = _____________________________________________ ;

  /* use a loop to read size number of values */

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

       scanf("%i", ______________________ );

  /* find minimum value in array */

min = _____________________ ;

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

if ( _____________ < min)

min = ________________________

/* print smallest value */

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

/* free DMA memory before end of program */

_____________________________

}