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 = ________3________________ , d = ______5___________________
a[0] = ________1______________ , a[1] = ________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",
____"r"__________________);
/* Read one integer value from
in.dat into val */
fscanf(_fileIn______________,"%i"
, &val);
/* close the input file */
fclose(____fileIn______________);
/* Open the
output file for writing */
fileOut = fopen("out.dat",
___"w"________);
/* Write one
complete C statement to write val
to out.dat */
____fprintf(fileOut, "%i ", val);_____________
/* close the output file */
fclose(_____fileOut_________________________);
}
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] );
}
_________1_______________0_________________________________
_________2_______________3_________________________________
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(___dept__ , ___100___
, __sizeof(Employee)_____, ___comp_empAsc______);
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