Department of Computer Science
Computer Science 101: Final Exam (120 minutes)
![]()
Name: NetID:
Lab Section: Date: December 16, 2009
![]()
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 15 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.
Section |
Possible Score |
Deduction |
Grader |
|
16 |
|
|
|
|
2 |
16 |
|
|
|
3 |
12 |
|
|
|
4 |
15 |
|
|
|
5 |
12 |
|
|
|
6 |
16 |
|
|
|
7 |
16 |
|
|
|
8 |
12 |
|
|
|
9 |
16 |
|
|
|
10 |
15 |
|
|
|
11 |
12 |
|
|
|
12 |
16 |
|
|
|
13 |
15 |
|
|
|
14 |
17 |
|
|
|
15 |
15 |
|
|
|
16 |
14 |
|
|
|
17 |
15 |
|
|
|
Total |
250 |
|
|
|
|
|
Exam Scoreà |
|
A
1. The following program compiles and runs without errors. Write the output of this program.
#include
<stdio.h>
int x = 1;
int y = 3;
void func(void)
{
printf("
In func: x = %i , y = %i\n", x , y);
}
void main(void)
{
int y = 7;
x = 3;
func();
printf("
In main: x = %i , y = %i\n", x , y);
}
In func: x = ___________, y = ___________
In main: x = ___________, y = ___________
2. Assume the following lines of code appear in a C program.
int a = 15;
int b = 4;
int *ptra = &a;
int *ptrb = &b;
b = (*ptrb)*(*ptra);
When run, the operating system stores the variable a at memory address 4000, 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.
Variable Value Location
|
a b ptra ptrb |
|
4000 |
|
|
4004 |
|
|
|
4008 |
|
|
|
4012 |
3. The program below compiles and runs without errors. Write the output of this program.
#include <stdio.h>
int fun(int *x, int y)
{
int z;
*x = y;
y = *x;
z = (*x)+ y +
5;
printf("%i %i %i \n", *x, y, z);
return z;
}
void main(void)
{
int a, b, c;
a = 1;
b = 3;
c = 5;
b =
fun(&a, c);
printf("%i %i %i \n", a, b, c);
}
_______ _______
_______
_______ _______ _______
4. Given the following variable declarations:
int x;
int * y;
float * z;
Circle each of the following assignment statements that will produce a compile ERROR.
a) x = 5;
b) y = &x;
c) y = *x;
d) *z = &x;
e) y = *z;
5. You are given the variable ptr declared below. Using either of the C functions calloc or malloc, write a single C statement to allocate memory for an array with the name ptr that holds 200 values of data type int.
int * ptr;
_____________________________________________________________
6. The following program compiles and runs without errors. Write the output of the program.
#include <stdio.h>
void swap3(int* x, int* y)
{
int temp = *x;
*x = (*y)*(*x);
*y = temp;
}
void swap2(int* x)
{
x[0] = x[2];
x[1] = x[0];
x[2] = x[1];
}
void swap1(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void main(void)
{
int a[3]={15, 11,
12};
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]);
a[0] = 15;
a[1] = 11;
a[2] = 12;
b = &a[1];
swap3(&b[0],
&b[1]);
printf("%i
%i\n", b[0], b[1]);
}
__________ __________ __________
__________ __________ __________
__________
__________
7. The following program compiles and runs without errors. Write the output of the program.
#include <stdio.h>
typedef struct
{
int length;
float weight;
} Cat;
void ptrChange(Cat *c)
{
c->length = 12;
c->weight = 3;
}
void nonPtrChange(Cat c)
{
c.length = 9;
c.weight
= 7;
}
void main(void)
{
Cat
c1,c3;
Cat
*c2;
c1.length
= 10;
c1.weight
= 5.7;
nonPtrChange(c1);
printf("length =
%i, weight = %f \n", c1.length, c1.weight);
c3.length = 15;
c3.weight
= 9.8;
c2
= &c3;
ptrChange(c2);
printf("length =
%i, weight = %f \n", c2->length, c2->weight);
}
length = ________________ , weight =
________________
length = ________________ , weight =
________________
8. The program below compiles without errors. Write the output of this program.
#include <stdio.h>
void swap(int *a, int *b)
{
*a
= *b;
*b
= *a;
}
void fun(int *a, int *b, int *c)
{
int
tmp = *a;
*a
= *b;
*b
= (*c)*(*a)+(*c)*(*b);
*c
= tmp;
}
void main(void)
{
int
a[2]={ 0, 1};
int
b[2]={ 2, 3};
swap(
&a[0], &a[1] );
printf("%i
%i\n", a[0], a[1] );
a[0] = 0;
a[1] = 1;
fun(
&b[0], &b[1], &a[0] );
printf("%i
%i\n", b[0], b[1] );
printf("%i
%i\n", a[0], a[1] );
}
______________ ______________
______________ ______________
______________ ______________
9. Complete
the following program by filling in the blanks. The output of this program
should be:
IL 1956
IL 1953
IN 1957
IN 1955
MI 1954
WI 1959
WI 1958
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char state[3];
int year;
}Championship;
/* sort the array in ascending order by state. If a state won championships in more than one year then list the years in descending order. */
int comp(Championship *a, Championship *b)
{
int k;
k = strcmp(a->state, b->state);/* Hint : strcmp returns “0” */
/* when both the arguments are same. */
if(k ==____________)
return___________________________;
else
return
k;
}
void main(void)
{
int
i;
Championship a[7] = {{"IL",1953},{"MI",1954},{"IN",1955},
{"IL",1956},{"IN",1957},{"WI",1958},
{"WI",1959}};
qsort(a,
7, sizeof(a[0]), comp);
for(i=0; i < 7; ++i)
printf("%s %i\n",
a[i].state, a[i].year);
}
For the next question use the following constants and structure data types:
#define MAXLENGTH 40
#define MAXTOPPINGS 10
/*The sauce types*/
#define TOMATO 0
#define CREAM 1
#define PESTO 2
typedef struct
{
char
topping_name[MAXLENGTH];
int extra;
float price;
}Topping;
/*A structure describing a pizza*/
typedef struct
{
char
pizza_title[MAXLENGTH];
int diameter;
int sauce_type;
float price;
Topping
toppings[MAXTOPPINGS];
}Pizza;
10. The following variable is defined within the main function of a C program using the above structures:
void main(void)
{
Pizza
newPizza;
.
.
Circle each statement below that will produce a compile ERROR(in the context of the above definitions and declarations).
a) newPizza->sauce_type = CREAM;
b) (&newPizza)->sauce_type = 1;
c) TOMATO = (*newPizza).sauce_type;
d) newPizza.sauce_type = TOMATO;
e) newPizza.sauce_type = PESTO – 1;
11. Use the typedef mechanism to define a structure data type named NBAPlayer that will store information about a NBA player. The structure will require the following four fields: a name and a team field, each of which will hold a string of up to 40 letters long, an integer field named year that stores the year when this player joined NBA, and a PPG field which holds a real (float) number denoting the average points this player scores per game. Write the complete structure definition, including the typedef, for the NBAPlayer data type in the blank space below.
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
12. Suppose we define an array of datatype NBAPlayer(defined in the previous problem), named players. The information of 100 NBA players is stored in this array. Fill in the blanks below to call qsort to sort this array using the compYearAsc function.
qsort(___________,____________,____________,________________);
13. Suppose we have the following type definitions and variable declarations:
typedef struct
{
int x;
float y;
} A;
typedef struct
{
int z;
A *p;
A v;
} B;
A a =
{2,1.5};
B b =
{3, &a, {3,5.0}};
A *pa = &a;
B *pb = &b;
Circle each statement below that will produce a compile ERROR (in the context of the above definitions and declarations).
a) b.p.y = 6.0;
b) pb -> z = (&a) -> x;
c) (*pa).y = (pb -> p).y;
d) pa = b.v;
e) pb -> z = (pb -> p)-> x;
14. Suppose we have the following type definitions and variable declarations:
typedef struct
{
char name[40];
/* track name or song name */
int length;
/* in seconds */
}Track;
typedef struct
{
int
cdno; /* cd number */
char title[30];
char artist[20];
int year;
int num_tracks; /* number of tracks */
int quantity;
float price;
float sales[12];
/* cd sales ($US) over 12 months */
Track tracks[20]; /* info for each track */
} Album;
Album albums[10];
Album mycd;
Complete the C code below by filling in the following blanks. Hint: use strcpy to assign strings. Fill in each blank with a single C statement that performs the specific assignment.
/* assign 1001 to the cdno
field of the variable named mycd */
_____________________________________________________________
/* assign "Michael
Jackson" to the artist field of the variable named mycd */
_____________________________________________________________
/* assign 1234.56 to the last
element of the sales array of the variable named mycd */
_____________________________________________________________
/* assign the second track of
the first element of the albums array to the first track of mycd */
_____________________________________________________________
15. Complete the code for the function readAlbums by filling in the blanks below. The function readAlbums reads records from a file into the albums array, where each record (line of input) is in the format:
All The Way... A Decade of Song; Celine Dion; 13.99
where
All The Way... A Decade of Song
is the title
Celine Dion
is the artist and
13.99
is the price.
Also, Album and albums are defined below. Note these are different than in the previous question.
typedef
struct {
char title[33];
char artist[16];
double price;
}
Album;
Album
albums[1000];
Notice, the values in the record
are separated by semi-colons and the price has data type double. You may assume
that all the titles are less than 33 characters long, the artist is less than
16 characters long and that the array albums
can hold more records than are in the file.
int
readAlbums(char *filename, Album albums[])
{
int count = 0;
FILE * fileptr;
fileptr = fopen( filename ,
__________________ );
if (fileptr == NULL)
return -1;
while( EOF !=
fscanf(___________,"_______________________",
______________________________________ ,
______________________________________ ,
______________________________________) )
{
count++;
} /* end of while loop */
fclose(fileptr); /* close the file */
return count;
}
16. The
following program compiles and runs without errors. Write the output of this program.
#include
<stdio.h>
int
count = 0; /* global variable */
int
mystery(int n)
{
count = count + 1;
if (n == 1)
return
0;
else
return mystery(n/2);
}
void
main(void)
{
printf("%i ", mystery(64));
printf("%i \n", count);
}
_____________________ _____________________
17. Fill in the blanks to complete the program below. The program uses recursion to reverse the values in the array. The output of the program is:
8 7
6 5 4
3 2 1
#include <stdio.h>
void
reverse(int array[], int start, int end)
{
int
temp;
if
(start >= end) /* Voom!!! */
return;
else
{
temp = array[start];
array[start] = array[end];
array[end] = temp;
++start;
--end;
reverse(____________, ____________,
____________);
}
}
void
main(void)
{
int
i;
int
array[8] = {1,2,3,4,5,6,7,8};
reverse(array,
0, 7);
for(i=0;
i<8; ++i)
printf("%i ", array[i]);
}