Department of Computer Science
University of Illinois at Urbana-Champaign
Computer Science 101: Final Exam (120 minutes)
Name: NetID:
Lab Section: Date: 8/7/09
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.
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.
Question |
Possible Score |
Deduction |
Grader |
|
15 |
|
|
|
|
2 |
14 |
|
|
|
3 |
15 |
|
|
|
4 |
18 |
|
|
|
5 |
9 |
|
|
|
6 |
20 |
|
|
|
7 |
18 |
|
|
|
8 |
10 |
|
|
|
9 |
16 |
|
|
|
10 |
18 |
|
|
|
11 |
18 |
|
|
|
12 |
18 |
|
|
|
13 |
15 |
|
|
|
14 |
10 |
|
|
|
15 |
30 |
|
|
|
16 |
18 |
|
|
|
17 |
18 |
|
|
|
18 |
20 |
|
|
|
Total |
300 |
|
|
|
A |
|
Exam Scoreà |
|
1. Suppose that in your home directory there is a directory called lab9 and a file called lab9.c .
You are in the lab9 directory currently. (See the figure below) Write a single Unix command that
will move the
file lab9.c from your home directory to the lab9 directory.

________mv ../lab9.c . _______or mv ~/lab9.c lab9.c_other solutions possible_
2. Assume that the file 1ab9.c is now in your current directory.
a) Suppose you compile lab9.c
in this way:
gcc lab9.c
Write the command that would allow you to run the resulting executable program.
____________a.out_____________________________________________
b) Suppose you compile lab9.c in this way:
gcc lab9.c -o lab9
Write the command that would allow you to run the resulting executable program.
______________lab9__________________________________________
3. Assume that you have answered the previous two questions correctly. Suppose you want to forget all your troubles and delete the lab9 directory from your life. You are currently in your home directory. Use the Write a single Unix command to delete the lab9 directory and all of its contents.
_________________rm –r lab9________________________________________
4. The following C program compiles and runs successfully. Write the output that this program produces.
#include <stdio.h>
void main(void)
{
int a;
float b;
a = 7.0/3.0;
printf("%i \n", a);
a = 9 % 4;
printf("%i \n", a++);
b = 4 + 6.0 / 2;
printf("%.1f \n", b);
}
__________________2_______________________________
____________________1_____________________________
_____________________7.0____________________________
5. The following C program compiles and runs successfully. Write the output that this program produces.
#include
<stdio.h>
void main(void)
{
int x = 4;
int y = 4;
if ( (x <= y) && (x >= y) )
printf("Bingo");
else
printf("No go");
}
________________Bingo_________________________________
6. Circle each of
the choices that do NOT produce an infinite loop.
a)
int x = 2;
while( x < 5)
{
switch(x)
{
case 2: x = x + 3;
case 1: x = x – 3;
}
}
b)
int x = 2;
while( x > 5)
{
x = 7;
}
c)
int x = 2;
do
{
x = 7;
} while( x > 5);
d)
int x = 2;
do
{
if( x > 1)
break;
} while( x > 5);
7. The following C
program compiles and runs successfully. Write the output that this program
produces.
#include <stdio.h>
void enigma(int c, int d[])
{
c = c+1;
d[0] = d[0]+1;
}
void main(void)
{
int a = 1;
int b[] = { 2, 3 };
enigma(a,b);
printf("%i %i %i\n", a, b[0], b[1]);
}
______1_______ ______3_______ _____3________
8. Assume we have declared the two dimensional array A in the C language,
int A[3][2] = { 0, 1, 2, 3,
4, 5 };
Write the value of A[2][1] below.
___________5______________________________________
9. The
following C program compiles and runs successfully. Write the output that this
program produces.
#include
<stdio.h>
#include <string.h>
void main(void)
{
char hello[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
char mellow[] = "mellow";
printf(" %c %s \n", hello[1], mellow );
hello[0] = mellow[0];
mellow[5] = hello[5];
if(strcmp(hello,mellow) == 0)
printf("Equal!\n");
else
printf("Not equal!\n");
}
______________e mellow__________________________________
_______________Equal!__________________________________
10. Fill in the blanks to complete the following program to read ten integers from an input file and write them to an output file:
#include <stdio.h>
void main(void)
{
/* Declare file pointers "inputptr" and "outputptr" */
_____FILE * inputptr;____________________
_____FILE * outputptr;____________________
int j, i=0;
/* Open the file "input.dat" (contains ten integers) */
/* using inputptr , in read mode */
_____inputptr = fopen("input.dat","r");_______________
/* Open the file "output.dat" (empty) using outputptr,
/* in write mode */
_______outputptr = fopen("output.dat","w");________
/* Test if either files have not opened successfully. */
if ( _(outputptr == NULL) || (inputptr == NULL)__ )
{
printf("Open file error for input.dat or output.dat \n");
return;
}
/* Read the integers from input.dat file into j, then */
/* write them out to output.dat */
while( i < 10 )
{
___fscanf(inputptr, "%i", &j);____
___fprintf(outputptr, "%i ", j);___
++ i ;
}
/* Close the files */
______fclose(inputptr);___________________________
______fclose(outputptr);____________________________
}
11. Complete the following program by filling in the blanks. This program is supposed to dynamically allocate an array of data-type double. The program will prompt the user for the number of array elements that need to be allocated.
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
int num;
double * d_array;
/* prompt the user for the number of array elements */
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));_____
/* verify if the allocation was successful */
if ( ___d_array == NULL______ )
{
printf("Memory not allocated!\n");
return;
}
/* code for remainder of program not shown */
}
12. Suppose we have defined a
structure autoPart as follows:
typedef struct
{
char name[10];
double price;
} autoPart;
a) Write the code to declare an array of data type autoPart , the name of the array is Parts, and Parts has 10 elements.
________autoPart Parts[10];_________________________________
b) Assume that data has already been put into the Parts array. Complete the C function (below)by filling in the blanks. You must use the built-in C function strcmp to find and then print out the price of a part named “strut”. If this part is not in the array, then print “Not found”. At most one element in the array Parts has the name “strut”.
void PrintStrutPrice(autoPart Parts[])
{
int k;
for(k=0; k < 10; k++)
{
if ( _____strcmp(Parts[k].name, "strut") == 0_____)
{
printf("%lf \n", _____Parts[k].price______);
return;
}
}
printf("Not found\n");
}/* end PrintStrutPrice function*/
13. The following C program compiles and runs successfully. Write the output that this program produces.
#include <stdio.h>
int myfunction(int x, int y[], int *z){
int a = 2;
int b[2] = {4,5};
int c = 6;
x = a;
y[0] = 7;
y[1] = 8;
*z = 9;
return *y;
}
void main(void)
{
int a = 1;
int b[2]={1,2};
int c = 3;
int d;
d = myfunction(a, b, &c);
printf("%i %i %i %i %i\n",a,b[0],b[1],c,d);
}
______1_______ ______7_______ _______8______ ______9_______ _____7________
14. The following C program compiles and runs successfully. Write the output that this program produces.
#include <stdio.h>
int Flip(int k,int m)
{
if (k > 0 && m > 0)
return Flip(k-1, m) + Flip( k, m-1) ;
else
return 1;
}
void main(void)
{
printf("%i \n", Flip(1,2));
}
15. Given a structure type named City defined below, answer questions a, b, and c.
typedef struct
{
char Name[13];
double Area;
int Population;
char State[3];
} City;
a) Declare
an array of 100 elements named cities
of data type City.
____________City cities[100]; ____________
b) Write a compare function named compPopAsc to sort an array of cities by population (ascending order).
int compPopAsc( City * a, City * b)
{
return a->Population – b->Population;
}
c) Complete the C statement below that calls the C function qsort to sort the array of cities, declared above, by Population in ascending order. You may assume that cities holds 100 elements. Remember qsort has the form:
qsort(array_name, number_of_elements, size_of_an_element, compare_function_name);
qsort( _cities_,_100_,_sizeof(City)_,_compPopAsc__);
16. Given a structure type named Fraction defined below, answer questions a and b.
typedef struct
{
int Numerator;
int Denominator;
} Fraction;
a) Declare a variable named A to be of data-type Fraction and initialize it’s value to 3/4.
____Fraction A = {3,4};_______________________________
b) Complete the code for a function named MultFraction that takes two values of data-type
Fraction as input and returns the product of the two as a value of data-type Fraction.
Keep in mind that the product of two fractions is also a fraction. i.e. ½ * ¾ = 3/8
Fraction MultFraction( Fraction first, Fraction second)
{
Fraction result;
_result.Numerator = first.Numerator * second.Numerator;__
_result.Denominator = first.Denominator * second.Denominator;
return result;
}
17. Suppose we define a data type named Student and an array named students of Student structures as follows:
typedef struct
{
int id;
int credithours;
char name[32];
} Student;
Student students[10];
Remember array indexing begins with 0 in C.
a) Write a single line of C code to assign the value 60 to the
credithours
member(field) of the first element of the students array.
______students[0].credithours
= 60;_________________
b) Write a single line of C code to assign the value “John
Smith” to the name
member(field) of the first element of the students array.
__________strcpy(students[0].name,
“John Smith”);_______
c) Write a single line of code to give the fifth
element of the students array the same
values as the first element of students array.
________students[4] =
students[0];_________________
18. For the following structure:
typedef struct
{
char title[50];
char author[50];
int pub_date;
} Book;
and the declaration:
Book bookarr[10] = {{"Fourier Series","Tolstov",1962},
{"Quantum Physics","Gasiorowicz",1974}};
Which one of the following expressions (a thru e), when substituted for the blank in the C statement below, will result in printing the title of the first element in the bookarr array?
printf("%s",____________);
There may be more than one correct answer. Circle each correct answer.
a) bookarr.title[0]
b) bookarr[0].title
c) bookarr[0]->title
d) (*bookarr).title
e) bookarr->title