Department of Computer Science
University of Illinois at Urbana-Champaign
Computer Science 101: Final Exam (120 minutes)
Name: NetID:
Lab Section: Date: 5/14/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 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.
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 |
|
|
|
|
|
|
2 |
|
|
|
|
3 |
|
|
|
|
4 |
|
|
|
|
5 |
|
|
|
|
6 |
|
|
|
|
7 |
|
|
|
|
8 |
|
|
|
|
9 |
|
|
|
|
10 |
|
|
|
|
11 |
|
|
|
|
12 |
|
|
|
|
13 |
|
|
|
|
14 |
|
|
|
|
15 |
|
|
|
|
Total |
250 |
|
|
|
|
|
Exam Scoreà |
|
A
1. The following C program compiles and runs without errors. Write the output of this program.
#include <stdio.h>
void swap(int, int);
void main(void)
{
int x = 1;
int y = 2;
swap(x,y);
printf("x = %i , y = %i \n",x,y);
}
void swap(int y , int x)
{
return;
}
x = _________1___________ y = ________2____________
2. The following C program compiles and runs without errors. Write the output of this program.
#include
<stdio.h>
int j = 5;
int i = 6;
void func(void); /* prototype for function func*/
void main(void)
{
int j = 2;
func();
printf("i = %i j = %i\n",i ,j);
}
void func(void)
{
int i = 7;
j = 1985;
return;
}
i = ________6____________ j = ________2____________
3.
The following program compiles and runs without error.
Write the output of the program.
#include <stdio.h>
void main(void)
{
int x[] = {1, 2, 3, 4, 5, 6};
int i;
int *s1;
int *s2;
int *s3;
int temp;
s1 = x;
s3 = &s1[3];
s2 = &s3[1];
temp = s1[1];
s1[1] = s1[2];
s1[2] = s2[0];
s2[0] = s3[0];
s3[0] = temp;
for(i = 0; i < 6; i++)
printf("%i ", x[i]);
}
_____1_____ ____3______ ____5______ _____2_____ ____4______ _____6______
4.
The following program compiles and runs without error.
Write the output of the program.
#include <stdio.h>
#include <string.h>
typedef struct{
char name[32];
int age;
} animal;
void main(void)
{
animal pet = {"dog",3};
char * ptrA = "cat";
char * ptrB;
printf("
%s ", pet.name);
printf(" %c ", pet.name[1]);
strcpy(pet.name, ptrA);
ptrB = pet.name;
printf(" %c ", ptrB[0]);
printf("%c \n",*ptrB);
}
_____dog_________ _______o_______ _______c_______ ______c________
5.
The following program compiles and runs without error.
Write the output of the program.
#include <stdio.h>
void swap3(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void swap2(int* x)
{
int temp = x[0];
x[0] = x[1];
x[1] = x[2];
x[2] = temp;
}
void swap1(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void main(void)
{
int a[3]={11, 13, 17};
int* b;
swap1(a[0], a[1]);
printf("%i %i %i\n", a[0], a[1], a[2]);
swap2(a);
printf("%i %i %i\n", a[0], a[1], a[2]);
b = &a[1];
swap3(&b[0], &b[1]);
printf("%i %i\n", b[0], b[1]);
}
_______11_________ ______13__________ _____17___________
_______13_________ ______17__________ _____11___________
_______11_________ ______17__________
6. Assume the following lines of code appear in a C program.
int a = 7;
int b = 49;
int *ptra = &a;
int *ptrb = &b;
int ans;
ans = *ptrb / *ptra;
When run, the operating system stores the variable “a” at memory address 2036, and the remaining variables are located at the addresses shown in the figure below.
Fill in the following diagram with the values stored in memory after the code above executes.
--------------------
a | | 2036
| ____7___________ |
--------------------
b | | 2040
|_____49__________ |
--------------------
ptra | | 2044
|_____2036________ |
--------------------
ptrb | | 2048
| ____2040________ |
--------------------
ans | | 2052
|_____7___________ |
--------------------
| |
7. Which of the following C statements will dynamically allocate an array named arr that will hold
100 elements of data type double? There may be more than one correct answer. Circle each correct answer.
a) double * arr = calloc(100,sizeof(double));
b) double arr = malloc(100*sizeof(double));
c) double * arr = calloc(sizeof(double),100);
d) double * arr = malloc(100*sizeof(double));
e) double arr = calloc(100,sizeof(double));
8. Fill in the blanks to sort the array named a , that has three elements in the order defined by the
function named comp. Next, write the output this program produces.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int numerator;
int denominator;
} Fraction;
int comp(Fraction * s, Fraction * t)
{
return ((s -> numerator) % (s -> denominator)) -
((t -> numerator) % (t -> denominator));
}
void main(void)
{
Fraction a[3];
int i;
a[0].numerator = 7;
a[0].denominator = 9;
a[1].numerator = 5;
a[1].denominator = 9;
a[2].numerator = 11;
a[2].denominator = 9;
/* qsort(arrayname , num_elts ,size_of_elts , compare_function) */
qsort(__a___,__3___,__sizeof(Fraction)_,_comp__);
for(i = 0; i < 3; i++)
printf("%i/%i \n",a[i].numerator, a[i].denominator);
}
________________11/9____________________________________________________
_________________5/9___________________________________________________
________________7/9____________________________________________________
9. Assume we have the following type definition:
typedef struct {
int x;
int y;
} myStruct;
and the following variable declarations and initializations:
myStruct a;
myStruct * p = &a;
int x = 0;
Which of the following is a correct statement for assigning the value 7 to the x field of the variable named a? That is, which of the following choices does the same thing as the following line of code?
a.x = 7;
Please circle each correct answer. There may be more than one correct answer.
a) (*a).x = 7;
b) (&a).x = 7;
c) (*a) -> x = 7;
d) (*p) -> x = 7;
e) p -> x = 7;
f) p[x].x = 7;
g) (&a) -> x = 7;
Use the following data type to answer the next question.
typedef struct
{
char type[25];
char jelly[25];
char bread[25];
int layers;
} pbj;
10. Suppose we need to manage a number of peanut butter and jelly sandwiches for a party. We
aren't expecting
more than 500 sandwiches. So, we'll declare an array of 500 sandwiches:
pbj sandwiches[500];
a) Write a single line of C code to initialize the first element of the sandwiches array so that the
peanut butter type is “smooth”.
.
_____strcpy(sandwiches[0].type, “smooth”);______________
b) Write a single line of C code to initialize the first element of the sandwiches array so that the
number of layers equals 2.
_______sandwiches[0].layers = 2;________________________________________
c) Write a single line of C code to assign all the fields (members) of the first element of the sandwiches array to the 500th element of the sandwiches array.
________sandwiches[499] = sandwiches[0];_______________________
11. Write C code to create a structure definition for Student, a structure with two fields
(members) of integer data type called id and credithours. Use typedef to name the structure
type Student.
___typedef struct{______________
_____int id;_________________________
_____int credithours;_________________________
___} Student;___________________________
Define your structure in such a way that you can declare a structure variable
of type Student as follows:
Student x ;
12. The information (name and age) of NBA players is stored in the file “player.dat”, as follows:
LeBron James; 24
Eddie House; 30
Kobe Bryant; 30
Fill in the blanks to complete the following program, which should read contents from the file
“player.dat” and write the information of Kobe Bryant to the file named “kobe.dat”. Note
that you cannot use “%s” to read the player name since the names contain spaces.
#include <stdio.h>
void main(void)
{
FILE *fin;
FILE *fout;
char name[50];
int age;
fin = fopen("player.dat", ____"r"__);
fout = fopen("kobe.dat", ____"w"______________);
while(EOF != fscanf(__fin_,__" %[^;]; %i”_,_name_,_&age__))
{
if(__strcmp(name, "Kobe Bryant"_)
fprintf(_fout_,"%s; %i",_name__,__age_);
}
/* close the input file */
fclose(___________________);
/* close the output file */
fclose(___________________);
}
13. The following C program compiles and runs without errors. Write the output of this program.
#include <stdio.h>
int func(int x, int y)
{
if (x*x == y)
return x;
else if ( x*x < y)
return func(x+1,y);
else
return func(x-1,y);
}
void main(void)
{
printf("%i ", func(1,1));
printf("%i ", func(2,9));
printf("%i ", func(5,16));
printf("%i ", func(1,64));
}
_______1__________ _______3__________ _______4__________ ______8___________
14. The
following C program compiles and runs without errors. Write the output of this
program.
#include
<stdio.h>
int mystery( int n)
{
if ( n <= 1) /* Voom! */
return n;
else
return mystery(n -
3);
}
void main(void)
{
printf(" %i ", mystery(5));
printf(" %i ", mystery(6));
printf(" %i ", mystery(7));
}
_____-1_______
______0______ _____1_______
15. Complete the following program by filling in the blank on this page and the blanks on the next
page. The compare function should tell the qsort function to sort the array named tracks in
ascending order by the value in the name field. When the program is completed, compiled and
run it should display the following output,
Another Side of Bob Dylan
All I Really Want To Do 400
Black Crow Blues 320
Time Out of Mind
Dirt Road Blues 500
Love Sick 320
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char name[40];
int length;
} Track;
typedef struct {
char title[30];
Track tracks[2];
} Album;
int compare(Track * a, Track * b)
{
return ___strcmp(a->name, b->name)____________________ ;
}
/* code continues on the next page */
void main(void)
{
Album albums[2];
int i,j;
strcpy(albums[0].title, "Another Side of Bob Dylan");
strcpy(albums[0].tracks[0].name, "Black Crow Blues");
albums[0].tracks[0].length = 320;
strcpy(albums[0].tracks[1].name, "All I Really Want To Do");
albums[0].tracks[1].length = 400;
strcpy(albums[1].title, "Time Out of Mind");
strcpy(albums[1].tracks[0].name, "Love Sick");
albums[1].tracks[0].length = 320;
strcpy(albums[1].tracks[1].name, "Dirt Road Blues");
albums[1].tracks[1].length = 500;
/* for each album sort the track names in ascending order */
for(i = 0; i < 2 ; ++i)
qsort(_albums[i].tracks_,__2____,sizeof(Track),compare);
for(i=0; i < 2 ; ++i)
{
printf("%s \n", albums[i].title);
for(j = 0; j < 2; ++j)
{
printf(" %s ", albums[i].tracks[j].name);
printf("%i \n", albums[i].tracks[j].length);
}
printf("\n");
}
}