Department of Computer Science
Computer Science 101: Final
Exam (120 minutes)
![]()
Name: NetID:
Lab Section: Date:
![]()
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.
You may not use any electronic devices, book, notes or other references during this examination.
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.
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.)
Section |
Possible Score |
Deduction |
Grader |
|
1 |
16 |
|
|
|
2 |
18 |
|
|
|
3 |
16 |
|
|
|
4 |
18 |
|
|
|
5 |
16 |
|
|
|
6 |
16 |
|
|
|
7 |
16 |
|
|
|
8 |
20 |
|
|
|
9 |
10 |
|
|
|
10 |
6 |
|
|
|
11 |
8 |
|
|
|
12 |
8 |
|
|
|
13 |
16 |
|
|
|
14 |
18 |
|
|
|
15 |
12 |
|
|
|
16 |
16 |
|
|
|
17 |
20 |
|
|
|
Total |
250 |
|
|
|
|
|
Exam Scoreà |
|
A
1. The program below compiles without errors and executes without run-time errors. Fill in the blanks with the output of this program.
#include <stdio.h>
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void funky(int e[], int * f)
{
e[0] = f[0];
e[1] = *f;
}
void main(void)
{
int a[2]={9, 8};
int b[2]={1, 2};
int
c = 3, d = 5;
swap(c, d);
printf("c = %i, d = %i\n", c, d);
funky(a,b);
printf("a[0]
= %i, a[1] = %i\n",
a[0], a[1]);
}
c = _______________________ , d = ________________________
a[0] = _____________________ , a[1] = ______________________
2. Fill
in the blanks to complete the following program, which should read a single
integer from the file named in.dat and write the same integer to the file
named out.dat .
#include <stdio.h>
void main(void)
{
FILE *fileIn;
FILE *fileOut;
int val;
/* Open the
input file for reading */
fileIn = fopen("in.dat",
______________________);
/* Read one integer value from
in.dat into val */
fscanf(_____________________,"%i" , &val);
/* close the input file */
fclose(__________________);
/* Open the
output file for writing */
fileOut = fopen("out.dat",
______________);
/* Write one
complete C statement to write val
to out.dat */
___________________________________________________
/* close the output file */
fclose(____________________________________________);
}
3. The program below compiles without errors and executes without run-time errors.. Write the output of this program.
#include <stdio.h>
void swap(int *a, int *b)
{
int tmp
= *a;
*a = *b;
*b = tmp;
}
void tripleswap(int *a, int *b, int *c)
{
int tmp
= *a;
*a = *b;
*b = *c;
*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] );
tripleswap(
&b[0], &b[1], &b[0] );
printf("%i %i\n", b[0], b[1] );
}
_________________________________________________________
________________________________________________________
4. Assume x is an array as declared below. Circle each of the following expressions that have the same value as the address of x[0] . There may be more than one correct answer.
double x[400]= { 0.0
};
a) x
b) *x
c) &x[0]
d) *x[0]
e) x[0]
f) 400
In the next two
questions you will use the code shown below.
typedef struct {
int
employeeID;
char firstname[50];
char lastname[50];
float salary;
int
deptID;
}Employee;
Employee dept[100];
5.
Fill in the blanks below to call qsort to sort the array named dept using the comp_empAsc
function you will write in the next question. Assume that you have 100
employees in the dept array.
qsort(___________ , ___________
, _______________________, ____________________);
6.
Write a function named comp_empAsc that qsort will
call to sort an array named dept of Employee data type (defined above) . You want to sort the array by deptID in ascending order . However, if any two
employees have the same deptID then they should be
sorted by their last names in ascending order (
Jones before Smith). Assume that qsort calls comp_empAsc. Do not write a complete
C program. Do not write any other code besides the code for comp_empAsc.
int comp_empAsc(Employee *ptr1,
Employee *ptr2)
{
/* your code goes here */
}
7. Fill in the table with the values that are assigned to the variables x , y , ptr1 and ptr2 after the following code fragment is executed:
int x = 3;
int y = 4;
int * ptr1;
int * ptr2;
ptr1 = &x;
ptr2 = &y;
*ptr1 = 5;
*ptr2 = 8 + *ptr1;

8. Given the declarations below complete the C code by filling in the blanks with the correct answers. Although we initialize y = {1.0, 2.0} your code should work correctly for any pair of numbers that are assigned to y.
#include <stdio.h>
typedef struct {
double real; /* real part of the complex number */
double imag; /* imaginary part of the complex number */
} complex;
/* prototype, mult will
multiply two complex numbers
*/
complex mult(complex *, complex *);
void main(void)
{
complex x, y = {1.0, 2.0}, z;
/* assign all values of fields of y to
the corresponding fields of x */
_________________________________
z = mult(&x,&y); /*perform the "complex" mult of x and y and assign to z */
printf("
real(z) = %lf \n", ____________________________________);
printf(" imag(z) = %lf \n", ____________________________________);
}
/* to multiply (mult
for short) two
complex numbers use the rule
{real1,imag1} * {real2,imag2} =
{real1 * real2 - imag1 * imag2 , real1 * imag2
+ real2 * imag1 }
so {1.0,2.0} * {1.0,2.0} = {-3.0,4.0}
*/
complex
mult(complex * a , complex * b)
{
complex c;
/* Do NOT just enter -3.0 */
c.real = __________________________________________________;
/* Do NOT just enter 4.0 */
c.imag = __________________________________________________;
return c;
}
9.
Use the typedef mechanism to define a
structure data type named Car that will store information about a
car. The structure will require the
following four fields: a make and a color field, each of which
will hold a string of up to 10 letters long, an integer field named year
that stores the year the car was manufactured, and a price field which
holds a real (float) number, such as 19999.95.
Write the complete structure definition, including the typedef,
for the Car data type in the blank space below.
typedef struct{
char
make[11];
char color[11];
int year;
float price;
} Car;
10.
The program below compiles and executes without
errors. Write the output of this program.
#include <stdio.h>
typedef struct {
int a[10];
int b[10];
} pair;
void eleven(int a[], int b[])
{
int j;
for (j=0; j<10; j++)
a[j] = 11 % b[j];
}
void main(void)
{
int i;
pair z;
for (i=0; i<10;
i++)
z.a[i] = i + 1 ;
eleven(z.b, z.a);
printf ("%i\n",
z.a[3]);
}
_______________________________________________________
11. There is a compile error in the following code. The program should output two equal values. Write down the line number of the line that contains the error and write the corrected version.
(Note: the line numbers are not part of the code)
1: #include <stdio.h>
2:
3: typedef struct {
4: int a;
5: int b;
6: } blah;
7:
8: void main (void)
9: {
10: blah a;
11: blah b;
12: b.a = 10;
13: b.b = 20;
14: a = &b;
15: printf ("%i = %i\n", b.a, a->a);
16: }
Line#_________ Corrected
Line: _______________________________________
12. Fill in the blanks below to assign random
numbers from 0 to 999 to the fields named a and b
in the mat array. Both fields a and b should be
completely filled.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a[100];
int b[100];
} arrays;
void main (void) {
int i,j;
arrays mat[10];
for (i=0; i<10;
i++) {
for (j=0; j<100; j++) {
_____________________________
= rand()%1000;
/* fill in a's */
_____________________________ = rand()%1000; /* fill in b's */
}
}
}
13. The following program asks the user to enter the midterm score and uses the print_midterm_score function to print the midterm score.
#include <stdio.h>
typedef struct
{
int midterm;
int final;
} test;
void print_midterm_score (test *sometest)
{
printf ("midterm score = %i\n", ________________________________ );
}
void main (void)
{
test mytest;
printf ("What score did you
get on your midterm:");
scanf ("%i",
&mytest.midterm);
print_midterm_score (&mytest);
}
Circle each answer that when
placed in the blank line above makes the program compile
without errors and
correctly output the midterm score. There may be more than one correct answer.
Do NOT fill in the blank above, just circle each
correct answer below.
a)
sometest->midterm
b)
sometest.midterm
c)
(*sometest)->midterm
d)
(*sometest).midterm
14. Examine the following complete program, and answer the question that follows. The line numbers are not part of the program. The purpose of the program is to create an array of 100 integers, assign the values 0, 1, 2, 3, …, 99 to the elements of the array and then print the values of these elements.
1: #include <stdio.h>
2: #include <stdlib.h>
3:
4: void main(void)
5: {
6: int * ptr;
7: int i;
8: ptr = malloc(100,
sizeof(int));
9: for (i=0; i<100; i++)
10: ptr[i] = i;
11: for (i=0; i<100; i++)
12: printf(" %i \n", ptr[i]);
13: fclose(ptr);
14: }
Answer the following question,
circling each correct answer. There may be more than one correct answer.
Are there errors in this program?
a.
Yes on line 9 and line 11, where i<100 should be i<=100.
b.
Yes, on line 6, where ptr must be declared as an int as opposed to int * .
c.
Yes, on line 8, where malloc should be calloc.
d.
Yes, on lines 10 and 12, where the code ptr[i] should be
written *ptr .
e.
Yes, on line 13, where fclose(ptr); should be written as fptr(ptr);.
f.
No errors.
15. Fill in the blanks to complete code for a function named remainder. The function remainder has two integer parameters named a and b . You may assume that both parameters are always assigned positive integer values.
The function remainder returns an integer value equal to the
remainder of the division of b by a. For example,
#include <stdio.h>
int remainder(int b , int a);
void main(void)
{
printf(" %i \n", remainder(2,5));
printf(" %i \n", remainder(8,3));
}
prints the values 2 2. However your code should work correctly not just for (2,5) and (8,3) but for any pair of positive integers.
You cannot use either the modulus (%)operator
or the division (/) operator in the function remainder. Use of the modulus (%) operator or the division (/)
operator will give you an automatic zero points score for this problem.
Hint: Use recursion. Note that remainder(8,3) =
remainder(8-3,3).
int remainder(int b , int a)
{
if ( b < a ) /* Voom!!!
*/
return
______________________;
else
return
____________________________________________ ;
}
16. The program below compiles and executes without errors. Write the output of this program.
#include <stdio.h>
int fn(int a, int b)
{
if (b == 1)
return a;
else
return a * fn(a,
b-1);
}
void main(void)
{
printf(" %i \n",
fn(4,1));
printf(" %i \n",
fn(4,2));
printf(" %i \n",
fn(4,4));
printf(" %i \n", fn(6,16));
}
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
17. From MP 2, we defined a structure named Album as follows:
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[MONTHS]; /* cd sales ($US)over the 12 months in 2005 */
Track tracks[20]; /* info for each track */
} Album;
Complete the code for the function named searchTitle by filling in the blanks. As a reminder, the MP 2 instructions state:
void searchTitle( Album albums[], int num_albums ) in "search.c"
Input parameters:
an array of albums and the number of albums
Return: None
This function first asks the user for a search string by printing the message "Enter a title: " and then scanning the string using " %[^\n]" in scanf (in order to accept blanks in the string). Then, it goes through every album in the list, and tries to match the search string with the album title. If the search string matches the title (or part of the title), the matching album should be displayed on screen (using the displayOneAlbum function). If there is no album matched, a message "Not found!\n" should be printed. You should use strstr function to perform the matching. The prototype of strstr is as follows.
char *strstr(const char *haystack, const char *needle)
The
function looks for needle in haystack, and returns NULL if it's not there.
void searchTitle( Album albums[], int num_albums )
{
int i ,
found = 0;
char title[40];
printf("Enter
a title: ");
scanf("
%[^\n]", title);
for(i=0; i < num_albums ; ++i)
if
( ______________________________ !=
_____________________________)
{
displayOneAlbum(&albums[i]);
___________________________________
}
if(
______________________________)
printf(______________________________);
}