Department of Computer Science
Computer Science 101: Final Exam (120 minutes)
![]()
Name: NetID:
Lab Section: Date: December 16, 2008
![]()
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 13 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 |
|
16 |
|
|
|
|
2 |
9 |
|
|
|
3 |
14 |
|
|
|
4 |
15 |
|
|
|
5 |
16 |
|
|
|
6 |
15 |
|
|
|
7 |
10 |
|
|
|
8 |
14 |
|
|
|
9 |
12 |
|
|
|
10 |
8 |
|
|
|
11 |
20 |
|
|
|
12 |
15 |
|
|
|
13 |
16 |
|
|
|
14 |
16 |
|
|
|
15 |
17 |
|
|
|
16 |
19 |
|
|
|
17 |
18 |
|
|
|
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 mystery(int a[], int b)
{
int i;
for(i = 0 ;
i < b ; ++i)
a[i] =
a[i] + 1;
b = 4;
}
void main(void)
{
int a[3] =
{1, 2, 3};
int b = 3;
mystery(a,
b);
printf("%i
%i %i %i \n", a[0], a[1], a[2], b);
}
___2___ ___3___ ___4___ __3____
2. The following C program compiles and runs without errors. Write the output of this program.
#include
<stdio.h>
void func(int * ptr,
int length, int x )
{
int i;
for (i = 0; i < length; i++)
{
ptr[i] = ptr[i] - x;
}
}
void main(void)
{
int a[3] = {2, 4, 6};
int b[3] = {2, 4, 6};
int c[3] = {2, 4, 6};
func(&a[0], 3, a[0]);
printf("%i %i %i \n", a[0], a[1],
a[2]);
func(&b[1], 2, b[1]);
printf("%i %i %i \n", b[0], b[1], b[2]);
func(&c[2], 1, c[2]);
printf("%i %i %i \n", c[0], c[1], c[2]);
}
___0___ ___2___ __4____
___2___ ___0___ ___2___
___2___ ___4___ ___0___
#include
<stdio.h>
void fun(int *a, int
b, int *c, int *d)
{
*c = (*a)* b;
*a = 5;
*d = 6;
printf("%i %i %i \n", *a, *c, *d);
}
void main(void)
{
int a, b, c;
int *d = &b;
a = 1;
b = 2;
c = 3;
fun(&a, b, &c, d);
printf("%i %i %i %i \n", a, b, c,
*d);
}
_____5_________ _______2_______ ______6________
______5________ _______6_______ ______2________ ______6________
#include
<stdio.h>
#include
<string.h>
void main(void)
{
int a, b;
char c, d;
a = 1;
b = 2;
char *ptr1 = "cs101";
char *ptr2 = "dog";
c = *(ptr1 + a);
d = ptr2[b - 2];
printf("%c ", c);
printf("%c ", d);
if (strstr(ptr1, ptr2) != NULL)
printf("A");
else
printf("B");
}
_______s___________ ________d__________ _______B___________
int a = 243;
int *b = &a;
When run, the operating system stores a at memory address 2036, and it stores b at memory address
2040. Fill in the following diagram with the values stored in memory at the two addresses.
| |
--------------------
| | 2036
| _____243________ |
--------------------
| | 2040
|______2036_______ |
--------------------
| |
#include <stdio.h>
void swap( int *c , int *d)
{
int i, temp;
for(i = 0; i < 3; ++i)
{
temp = _____c[i]__________ ;
c[i] = _____d[i]__________ ;
____d[i]________ = temp;
}
}
void main(void)
{
int a[]={1,2,3};
int b[]={4,5,6};
swap(______a_______ , ______b___________);
printf(" %i %i %i \n",
a[0],a[1],a[2]);
printf(" %i %i %i \n",
b[0],b[1],b[2]);
}
When the program is compiled and run it should display:
4 5 6
1 2 3
The next three questions refer to the following type definitions and variable declarations:
typedef struct
{
int a;
int b;
} S;
typedef struct
{
int c;
S d;
S *e;
} T;
S s = {1,2};
T t = {3,{4,5}, &s};
S *ps = &s;
T *pt = &t;
_______s.b = 5;_______________________
__________t.d.b
= 6;______________________________________________
a) pt -> c = (&s) -> a;
b) (*ps).b = (pt -> e).b;
c) ps = t.e;
a) char * treated = calloc(100*strlen(treated));
b) char * treated = calloc(100*sizeof(char));
c) char * treated = calloc(100*sizeof(treated));
d) char * treated = calloc(100,sizeof(char));
#include <stdio.h>
void main(void)
{
char filename_in[] = "input.dat";
char filename_out[] = "output.dat";
char format[] = "%i";
FILE *fpin, *fpout;
int i;
___fpin________ = fopen(___filename_in___________,"r");
___fpout________ = fopen(__filename_out____________,"w");
while (EOF != fscanf(_fpin__, _"%i"__, &i))
fprintf(__fpout_,_____"%i "_____, i);
fclose(___fpin__________);
fclose(____fpout_________);
}
#include <stdio.h>
void recprint (int num)
{
if (num == 0) /* Voom!!! */
printf("*");
else
{
printf(______"("________);
recprint(____num-1_________);
printf(")");
}
}
void main (void)
{
recprint(_____4________);
printf("\n");
}
a) #include
<stdio.h>
int harpo (int n)
{
if (n <= 0)
return 4;
else
return ( n-1 +
harpo(n) );
}
void main(void)
{
printf("%i
\n", harpo(4));
}
b) #include
<stdio.h>
int chico (int n)
{
if (n == 0)
return 0;
else
return ( n +
chico(n-1) );
}
void main(void)
{
printf("%i
\n", chico(4));
}
c) #include
<stdio.h>
int groucho (int n)
{
return ( 10 +
groucho(n/5) );
}
void main(void)
{
printf("%i
\n", groucho(4));
}
d) #include
<stdio.h>
int zeppo(int n)
{
if (n % 2 == 1)
return
10;
else
return ( zeppo(n/2) );
}
void main(void)
{
printf("%i
\n", zeppo(4));
}
Use the following C code for the
next three questions:
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[100];
Album
mycd;
/* qsort(arrayname , num_elts ,size_of_elts ,
compare_function) */
qsort(__albums_,__100_,
_sizeof(Album)_, _compareValueDesc_);
int
compareValueDesc( Album * l , Album * r )
{
float lvalue = ___l->price *
l->quantity___;
float rvalue = ____r->price *
r->quantity__;
if ( lvalue < ____rvalue__________________________
)
return
___1___ ;
else if ( _____lvalue > rvalue_____________)
return
__-1____;
else
return
__0____;
}
/* end of compareValueDesc */
____mycd.cdno = 1001; _____ /* assign
1001 to the cdno field of the variable named mycd */
_____strcpy(mycd.title, "One Heart");___ /* assign "One Heart" to the title field */
_____mycd.sales[0] =
1234.56; __* assign
1234.56 to the first element of the sales array */
typedef struct
{
double
x;
double
y;
}cartesian;
void main(void)
{
cartesian p1 = {1.00, 2.00};
cartesian * p2 = &p1;
cartesian * p3 = p2;
printf("y coordinate = %4.2lf \n",
_________________);
}
a) p1
b)
y
c)
*p2.y
d) p2->y
e)
p3.y
f)
(*p3).y