Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam  (120 minutes)

 


Name:                                                                         NetID:
 

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

12

 

 

2

10

 

 

3

24

 

 

4

18

 

 

5

20

 

 

6

12

 

 

7

16

 

 

8

16

 

 

9

10

 

 

10

12

 

 

11

12

 

 

12

12

 

 

13

12

 

 

14

16

 

 

15

12

 

 

16

12

 

 

17

24

 

 

Total

250

 

 

A

 

 

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

    #include <stdio.h>

            void func(int* x, int y)

     {

          *x = *x + 1;

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

     }

 

     void main(void)

     {

          int x;

          int y;

          x = 1;

          y = 3;

          func(&x,y);

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

     }

 

 

        ______2_______4_____________

 

 

        ______2_______3_____________

 

 

 

 

 

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

    #include <stdio.h>

#include <string.h>

     void main(void)

     {

          char a[] = “We love CS101”;

          char * b = “CS101”;

          char * c = b;

          

          printf(“%s \n”, c);

          if( strstr(a,b) != NULL)

              printf("%c \n",a[0]);

          else

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

     }

 

                   ______CS101____________________

 

 

                   ______W____________________

  1. Write a complete function named comp_asc such that when called by qsort, will sort the array named fractions in ascending order. Also, fill in the blanks so that the following program correctly calls qsort.  You may assume that no fraction is in the form 3/0  that is, you may assume that the denominator is never 0.  You may also assume that  no fraction is in the form 3/-5, that is the denominator is never negative, although -3/5 is valid.  Hint: a/b < c/d if and only if  a*d < c*b . That is, 2/5 < 3/4 since 2 * 4 < 3 * 5. 

           

#include <stdio.h>

#include <stdlib.h>


            typedef struct

     {   

          int num;
          int denom;

     } fraction;

 

     int comp_asc(fraction * ptr1, fraction * ptr2)

     {

          /*Fill out the code for the compare function here*/

 

 

        if (ptr1->num * ptr2->denom < ptr2->num * ptr1->denom)

            return -1;

        else if (ptr1->num * ptr2->denom > ptr2->num * ptr1->denom)

            return 1;

   else

            return 0;

 

or

        return ptr1->num * ptr2->denom - ptr2->num * ptr1->denom ;

           

     

 

     }

    

 

 

     void main(void)

     {

       fraction fractions[5]= { {1,2},{2, 3},{4,5},{-1,3},{-1,2}} ;

       int i;

 

    

       qsort(_fractions__,_5_,_sizeof(fraction)_,_comp_asc_);

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

 printf("%i/%i\n", fractions[i].num,

                               fractions[i].denom);

     }

 

 

 

 

 

  1. The contents of the file 'input.dat', are as follows:

 

Oldsmobile Intrigue, 3

Toyota Prius, 2

Volkswagen Beetle, 5

 

         The following code prints out the contents of the file to the screen.  Fill in the blanks to

         enable the program to read the file.  Make sure to read the contents into the variables name and

         number.  The comma is used as the divider between car name and number and not the semi-colon (;) as      

         on your MP 2. Note that you cannot use “%s” to read the car name since the names contain spaces.

 

 

            #include <stdio.h>

void main(void)

     {

          char name[50];

          int number;

                   

          /* Open the file input.dat for reading */

          FILE* fptr = ____fopen(“input.dat”, “r”)_______;

 

          /* Read data from the file */

          while(EOF !=  __fscanf(fptr,” %[^,], %i”, name, &number)_)

          {

            printf("name = %s, number = %i\n",name, number);

          }

 

          /* Close the file */

 

          _____fclose(fptr)___________;

     }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

 

#include <stdio.h>

 

typedef struct { 

float real; 

float imaginary;

} complex;

 

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

 

void main(void)

{ 

complex x, y = {1.0, 2.0}, z; 

x = y;  /* assigns all values of fields of y to x */

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

/* print the real and imaginary parts of z */
     /* DON’T write 2.00 , 4.00 as your answer */ 

printf(" real(z) = %.2f, imag(z) = %.2f \n",

       __z.real__, __z.imaginary___);

}

 

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

/* sum should work when a and b are any complex numbers */

     complex c = {_a.real + b.real__ , __a.imaginary + b.imaginary_};

return  c;    

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Write C code to define a new data type called Movie, a structure to store  information about movies. It should have the following four fields: a  title and a producer field, each of which will hold a string of up to 100 letters long, an integer field named year that stores the year the movie was produced, and a length field(which holds a real number, such as 153.95) storing the length of the movie in minutes.  Write the complete structure definition in the blank space below.

 

 

       typedef struct

   {

    char title[101];

    char producer[101];

    int year;

    float length;

  } Movie;

 

 

 

 

 

 

 

 

 

  1. You are given the variable myMovie of the type Movie as shown below (the Movie datatype in this question is the one that you created in the previous question). Write your answer for the following questions. Hint: You may use any C library function in your answer.

 

 

Movie myMovie;

 

a) Write a single statement of C code to assign the value "Ice Age 2" to the title field of myMovie.

 

 

____strcpy(myMovie.title, “Ice Age 2”);_________________________

 

 b) Write a single statement of C code to assign the value 153.95 to the length field of myMovie.



____myMovie.length = 153.95;_____________________

 

c) Write a single statement of C code to declare a new variable of data type Movie with the name anotherMovie.

 

____Movie anotherMovie;_______________________________

 

 

d) Write a single statement of C code to assign the value of the title field of myMovie to     

     the title field of anotherMovie.

 

_____strcpy(anotherMovie.title, myMovie.title);___________________

 

  1. Fill in the blanks in the code below to allocate memory for an array named mem of 400 elements of integer datatype. Then assign the value 5 to each element in the array,  and finally release the memory.

               int i;

int * mem;

mem = malloc(_____400 * sizeof(int)___);


for (i=0; __i < 400_____________________ ; ++i)

{

    ____mem[i]______ = 5;

}

___free(mem);_________      /* Release the allocated memory */

 

 

 

  1. Assume x is an array as declared below. Which of the following expressions have the same value as the address of first element of the array?

 

double x[400];
  

Circle ALL correct answers.

a)  x

 

 

 

 

b)  *x

 

 

 

 

c)  x*

 

 

 

 

d)  *x[0]

 

 

 

 

e)  x[0]

 

 

 

 

f)  &x[0]

 

 

  1. Given the following code:

 

 

#include <stdio.h>

 

int b = 2;

 

int fun(int a, int b) {
     a = b – a;

     return a ;

}

 

void main(void) {

     int a = 1;

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

}

 

Write the output that the program produces:



_______1__2__-1____________________________________________

 

 

 

 

 

 

 

  1. Write the output of the following program:

 

#include <stdio.h>

 

int myVar = 2;

 

void fun(void) {

     myVar = myVar * 2;

     printf(“%i ”, myVar);

}

 

void main(void) {

     int myVar = 1;

     fun();

     printf("%i ", myVar);

}

 

Write the output that the program produces:

 

 

 

______4________1_________________________________________

 

 

 

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

#include <stdio.h>

int next(void) {

     static int twoPrev = 0;

     static int onePrev = 1;

     int new;

         

          new = twoPrev + onePrev;

     twoPrev = onePrev;

     onePrev = new;

     return new;

}

 

     void main(void){

          printf(“%i ” , next() );

          printf(“%i ” , next() );

          printf(“%i ” , next() );

          printf(“%i ” , next() );

     }



           _____1___2___3___5_________________________________________________________________

 

 

 

 

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

 

#include <stdio.h>

 

void func(int* , int*, int*);

 

void main(void){

  int x = 1;

  int y = 2;

  int z = 3;

 

  func(&x, &y, &z);

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

}

 

void func(int* a, int* b, int* c){

  int* temp = c;

 

  *c = *b;

  *b = *a;

  *a = *temp;

}

 

 

____x = 2  y = 1  z = 2__________________

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

 

#include <stdio.h>

 

void func(int* , int*, int*);

 

void main(void){

 
int x[2]={9,4};
int *p1, *p2;
int a = 9, b = 2;

printf("%i %i \n",x[0],x[1]);
p1 = x;
printf("%i %i \n",p1[0],p1[1]);
p2 = &p1[1];
printf("%i \n",p2[0]);
*p1 = a;
*p2 = b;
printf("%i %i \n",p1[0],p1[1]);
printf("%i \n",p2[0]);

}



____9________4_____________________


____9________4_____________________


____4_____________________________