Department of Computer Science

University of Illinois at Urbana-Champaign

Computer Science 101: Final Exam (120 minutes)

 


Name:                                                                         NetID:
 

Lab Section:                                                                Date: 5/09/11

 


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

12

 

 

2

8

 

 

3

18

 

 

4

18

 

 

5

12

 

 

6

12

 

 

7

16

 

 

8

12

 

 

9

18

 

 

10

12

 

 

11

9

 

 

12

10

 

 

13

15

 

 

14

14

 

 

15

15

 

 

16

16

 

 

17

18

 

 

18

15

 

 

Total

250

 

 

 

 

Exam Scoreŕ

 

A

 

 

 

 

1.      Which of the following expressions always have the same value as the address of the first element of the array named x, declared below?  Circle ALL correct answers. There may be more than one correct answer. 

 

double x[10];
 

a)
  x

 

 

b)  *x

 

 

c)  x*

 

 

d)  *x[0]

 

 

e)  x[0]

 

 

f)  &x[0]

 

 

 

 

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

 

#include <stdio.h>

 

void add (int, int);

 

void main(void)

{ 

  int i, total = 0;

 

  for (i = 1; i <= 4; i++)

      add(i,total);

 

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

}

 

void add (int x, int total)

{

  total = total+x;

}

 

__________0___________________________

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

 

#include <stdio.h>

 

void Rotate(int, int, int);

 

int x; 
int y;

   

void main(void)

     {

       int z = 3;

 

  x = 1;

  y = 2;

       Rotate(x, y, z);

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

     }

 

void Rotate(int x, int y, int z)

     {

  int temp = x;

 

       x = y;    

  y = z;

  z = temp;

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

     }

 

 

 

_____2_________  ______3________  _______1_______

 

 

 

______1________  _______2_______  _______3_______

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

void swap1(int* a, int* b)

{

  int temp = *a;

 

  *a = *b;

  *b = temp;

}

 

void swap2(int* a, int b)

{
  int temp = *a;


  *a = b;
   b = temp;
}

 

void main(void)

{

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

  int b[3] = {4, 5, 6};

  int c[3] = {7, 8, 9};

  int* ptr;

 

  swap2(&a[0], a[1]);

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

  ptr = &c[1];

  swap1(&ptr[0], &ptr[1]);

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

}

 

 

______2________  ______2________  ______3________

 

 

 

_______7_______  _____9_________  _______8_______

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

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

{

  int* temp = c;

 

  *c = *b;

  *b = *a;

  *a = *temp;

 }

 

void main(void)

{

  int x, y, z;

 

x = 2;

y = 1;

z = 3;

 

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

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

}

 

 

x = ______1________  y = ______2________  z = _____1_________

 

 

 

6.      Fill in the blanks with the corresponding letter that matches the return value from the compare function with the corresponding meaning of that value for the qsort function.

 

Return values from the compare function:

 

a)      negative (<0)

 

b)      positive (>0)          

 

c)      zero (0)

 

Meaning of the values sent from the compare function that qsort receives:

 

 

1.      swap the array values                                              ______b___________

 

2.      do not swap the array values                                   _______a__________

 

3.      array values are equal                                             _______c__________

Use the following C code for the next two questions:

 

typedef struct

{

  int    weight;

  char   make[30];

  char   model[20];

  int    year;

  float  price;

} Vehicle;

 

Vehicle cars[150];

 

 

 

 

7.      Fill in the blanks to call the qsort to sort the array named cars. You may assume that the array cars has been filled with 150 cars and that you have written a comparison function named compYearDesc that qsort will call to sort the array in descending order by year.

 

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

  qsort(_cars_,_150_,_sizeof(Vehicle) or sizeof(cars[0])_,_compYearDesc_);

 

 

 

 

 

 

8.      Fill in the blanks to complete the code for the function named  compYearDesc . The function qsort will call compYearDesc to sort an array in descending order by the value in the year field.

 

int compYearDesc( Vehicle * a, Vehicle * b)

{

 

   return   ___b___________ ->year  -  _______a_______ ->year;

 

}

 

 

 

 

 

 

 

 

 

 

 

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

 

#include <stdio.h>

 

void fun1(int * ptr, int length, int x)

{

  int i;

 

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

      ptr[i] = ptr[i] - x;

}

 

void main(void)

{

  int a[3] = {1, 3, 5};

  int b[3] = {2, 4, 6};

  int c[3] = {7, 8, 9};

 

  fun1(&a[0], 3, a[0]);

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

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

 

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

  fun1(&c[2], 1, c[2]);

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

}

 

 

______0________  ______2________  ______4________

 

 

 

______2________  ______0________  ______2________

 

 

 

______7________  _______8_______  ______0________

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

10.  Fill in the blanks to complete the code below to open a file with the name input.txt for reading.

 

FILE *fin;

 

____fin__ = fopen(_"input.txt"___,__"r"___);

 

 

11.  On the following line of code there are three modes that can be entered when opening the file named fileName.  Match the modes with the corresponding action fopen takes if the file named  fileName already exists.

 

fopen("fileName",_____);

 

    a. "r"

    b. "w"

    c. "a"

 

  ___b__ Opens fileName for writing and deletes contents.

 

 

  __c___ Opens fileName for appending and writes at the end of a file.

 

 

  __a___ Opens fileName for reading.

 

 

 

 

12.  Circle the value that the fopen function (shown below) returns if a file named fileName does not exist. There is only one correct answer.

   fopen("fileName","r")

 

a.       -1

 

 

b.      1

 

c.       NULL

 

 

d.      "r"

 

13.  Complete the following program below by filling in the blanks. This program will prompt the user for the number of entries that the user will enter. Next, the program dynamically allocates an array of data type double that will hold exactly the number of entries the user specified.

 

#include <stdio.h>

#include <stdlib.h>

 

void main(void)

{

  int num;

  /* declare d_array, with the correct data-type*/

 

  ___double *______    d_array;

  printf("Please enter the number of array elements: ");

  /* read the number of array elements into num */

       scanf("%i",&num);  

  /* dynamically allocate a block of memory */

 

  d_array =  ___malloc(num*sizeof(double); or calloc(num,sizeof(double);_____

  /* verify if the allocation was NOT successful */

 

  if (____d_array == NULL_____)

  {

      printf("Memory not allocated!\n");

      return;

   }

     /* more code after this point but not shown here */

 }

 

 

 

 

 

 

14.  Fill in the blanks to complete the code below that computes the lift and assigns it to the lift sub-field of the wing field of the variable named c . The value of alfa is also assigned to the same variable c .

The formula for the lift of a wing geometry given its Angle of attack (alfa) is:

                        lift = 2*PI*alfa 

           

            For example, running the following program should display the values   3.00   18.85 .

 

#include <stdio.h>

#define PI 3.141592653589793

 

typedef struct

{

  double alfa;

  double lift;

} wings;

 

typedef struct

{

  wings  wing;

  double weight;

  double cost;

} plane;

 

 

void main(void)

{

   plane   c;

   double  alfa = 3.0;

   double  lift = 2.0*PI*alfa; /* 18.85 */

   wings * ptrw = &(c.wing);

   plane * ptrp = &c;

 

        /* assign 3.0 to the alfa subfield of c */

        

      ________c.wing.alfa___ = alfa;

       

   /* assign 18.85 (the lift value) to the lift subfield of c */

 

 

   (____ptrp________ -> wing).lift = lift;

      

    printf(" %.2lf %.2lf \n", c.wing.alfa, c.wing.lift );

}

 

 

 

 

 

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


#include <stdio.h>

 

typedef struct

{

  char lname[255];

  int age;

} Student;




void printStd(Student * stdPtr)

{

  printf("%s ", ____stdPtr->lname__________);



  printf("%i" , _____stdPtr->age___________________________);

}

 

void main(void)

{

  Student students[10];

  int i;

 

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

  {

scanf("%s", _______students[i].lname_________);

 

scanf("%i", _______&students[i].age__________);
   }


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

     printStd(_____&students[i]_____);

     printf("\n");

   }

 }

 

 

 

 

 

 

 

 

 

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



   #include <stdio.h>

 

   int donald(int n)

   {

   if (n < 2)

     return n;

   else

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

   }

 

   void main(void)

   {

   int i;

 

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

     printf("%i ", donald(i));

 

   printf("\n");

   }

 

 

________0________   ________1________   ______1__________   _______2_________  



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

17.  The following program when completed should compute  xn (x not equal to zero), the integer power of real number, and compile with no errors.  It does so by reading in the real number x and the integer power n from the keyboard.  Fill in the following blanks so that the program will work correctly.

#include <stdio.h>

 

double power(double x, int n)

{

  if (n == 0)

return 1.0;  

  else  if (n>0)

            return x*power(x, ____n-1______________);

             else if(n<0)

return (1.0 /____x__________ )*power(x,__n+1___________);

}

 

void main(void)

{

     double x;

     int n;

    

     printf("Enter x: ");

     scanf("%lf", &x);

     printf("Enter n: ");

     scanf("%i", &n);

     printf("%lf \n", power(x, n));

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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("done ");

    return;

  }

  if (n > 1)

  {

    printf("you ");

    dream(n-1);

    printf("are\n");

  }

}

 

void main(void)

{

  dream(2);

}

 

 

 

 

___________you  done  are____________