Department of Computer Science
University of Illinois at Urbana-Champaign
Computer Science 101: Final Exam (120 minutes)
Name: NetID:
Lab Section: Date: 5/7/10
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 11 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 |
12 |
|
|
|
3 |
16 |
|
|
|
4 |
15 |
|
|
|
5 |
16 |
|
|
|
6 |
12 |
|
|
|
7 |
12 |
|
|
|
8 |
12 |
|
|
|
9 |
12 |
|
|
|
10 |
12 |
|
|
|
11 |
20 |
|
|
|
12 |
16 |
|
|
|
13 |
12 |
|
|
|
14 |
14 |
|
|
|
15 |
12 |
|
|
|
16 |
14 |
|
|
|
17 |
12 |
|
|
|
18 |
15 |
|
|
|
Total |
250 |
|
|
|
|
|
Exam Scoreà |
|
A
1. The program below compiles and runs without errors. Write the output of this program.
#include <stdio.h>
void func(int* x, int *y)
{
*x = *x - 1;
*y = *y + 1;
printf("%i %i\n",*x, *y);
}
void main(void)
{
int x;
int y;
x = 1;
y = -1;
func(&x,&y);
printf("%i %i\n",x,y);
}
_______0________ ______0_________
_______0________ ______0_________
2. The program below compiles and runs without errors. Write the output of this program.
#include <stdio.h>
void main(void)
{
int a[4] = {1,5,8,10};
int *q = a;
printf("%i ",*q);
printf(" %i",q[1]);
}
_______1________ ______5_________
3. Each of the following programs compiles and runs without errors. Circle each of the following four programs that will produce the output of “x = 2, y = 1”. There may be more than one correct answer.
|
a) Program 1
#include <stdio.h>
void swap(int x, int y) { int temp = x; x = y; y = temp; }
void main(void) { int x = 1; int y = 2; swap(x, y); printf("x = %i, y = %i\n", x, y); }
|
b) Program 2
#include <stdio.h>
int x; int y;
void swap(int x, int y) {
int temp = x; x = y; y = temp; }
void main(void) { x = 1; y = 2; swap(x, y); printf("x = %i, y = %i\n", x, y); }
|
|
c) Program 3
#include <stdio.h>
int x; int y;
void swap(void) {
int temp = x; x = y; y = temp; }
void main(void) { x = 1; y = 2; swap(); printf("x = %i, y = %i\n", x, y); }
|
d) Program 4
#include <stdio.h>
int x; int y;
void swap(int a, int b) {
x = b; }
void main(void) { x = 1; y = 2; swap(x,y); printf("x = %i, y = %i\n", x, y); }
|
4. Given the following variable declarations:
int a = 7;
int * b = &a;
int * c = b;
Circle each of the following code fragments which will produce a compile ERROR. There may be more than one answer circled.
a) a = 5.9;
b) *b = *c;
c) &a = 3000;
d) b->a = 5;
e) b.a = 5;
5. The program below compiles and runs without errors. Write the output of this program.
#include <stdio.h>
void swap3(int* x, int* y)
{
*x = (*y) + (*x);
*y = *x;
}
void swap2(int* x)
{
x[1] = x[0];
x[0] = x[1];
}
void swap1(int x, int *y)
{
x = 5;
*y = x;
}
void main(void)
{
int a[3]={15, 11, 12};
int * b;
swap1(a[2], a);
printf("%i %i %i\n", a[0], a[1], a[2]);
a[0] = 15;
a[1] = 11;
a[2] = 12;
b=a;
swap2(&b[1]);
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]);
}
__________5_____________ _________11____________ ________12_____________
__________15___________ ___________11___________ _________11____________
___________23____________ _______23________________
6. The program below compiles and runs without errors. Write the output of this program.
#include <stdio.h>
#include <string.h>
void main(void)
{
int a, b;
char c, d;
char *ptr1 = "cs101";
char *ptr2 = "final";
a = 3;
b = 4;
c = *(ptr1 + a);
d = ptr2[b - 2];
printf("%c %c \n", c, d);
if (strstr(ptr1, ptr2) != NULL)
printf("A \n");
else
printf("B \n");
}
_________0______________ __________n_____________
_________B______________
The next six problems refer to the following structure and array:
typedef struct
{
int ssn;
char FirstName[32];
char LastName[32];
char sex;
int height;
int weight;
int age;
} Student;
Student students[550];
7. Fill in the blanks in the following function. We want qsort to sort an array of data type Student in ascending order by the value in the LastName field.
int compLastNameAsc(Student *stu1, Student *stu2)
{
return strcmp(___stu1->LastName____,__stu2->LastName___);
}
8. Fill in the blanks in the following function. We want qsort to sort an array of data type Student in descending order by the value in the age field.
int compAgeDesc(Student *stu1, Student *stu2)
{
return ____stu2->age____ - _____stu1->age__________________ ;
}
9. Fill in the blanks in the call to qsort to sort all 550 elements of the array named students based on compAgeDesc.
qsort (__students__,__550__ ,_sizeof(Student)__ , compAgeDesc);
10. Fill in the blanks in the call to qsort to sort all 550 elements of the array named students based on LastName in ascending order.
qsort (_students_,__550_ ,_sizeof(Student)_ , _compLastNameAsc_);
11. Complete the code for the function named findSSN by filling in the blanks. This function is going to find a student having the given ssn. If it finds one, it returns the array index for that student. If it doesn’t find one it returns -1. Note that the ssn is unique for each student, so there is at most one student containing the given ssn number.
Input parameters: an array of students, the
number of students in the array, and the target ssn
Return: the array index of the student with the given ssn or -1 if there
is no such student
int findSSN(Student students[], int num_students, int ssn)
{
int index;
for(index = 0 ; ____index < num_students____
; ++index)
{
if
( ___students[index].ssn__ == ssn)
return ________index_____________;
}
return ______-1_____________;
}
12. Assume the following lines of code appear in a C program.
Student stu = {123456789, "Isadora","Duncan",’F’,60, 120, 21};
int * ptra = &stu;
char a = stu.sex;
int b = ptra -> height;
char * ptrb = &(*ptra);
When run, the operating system stores the variable stu at memory address 1000, 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 |
F |
4000 |
|
60 |
4004 |
|
|
1000 |
4008 |
|
|
1000 |
4012 |
The next three problems refer to the following C code:
#include <stdio.h>
#include <string.h>
typedef struct
{
int length;
char name[64];
} Track;
typedef struct
{
char title[40];
char artist[40];
int num_tracks;
Track tracks[10];
} Album;
Album mycd;
Album * albums;
13. The program below (including the C code shown above) compiles and runs without errors. Write the output of this program.
void main(void)
{
Track t1 = {165, "Let’s Face the Music and Dance"};
Track t2 = {171, "The Coffee Song"};
Album a = {"Ring-a-Ding-Ding!","Frank Sinatra",2 , {t1,t2} };
Album * b = &a;
printf(" %i %c \n", t1.length, t1.name[0]);
printf("%s %i \n", a.artist, a.tracks[1].length);
printf("%s %i \n", (b->tracks[1].name),(*b).tracks[0].length);
}
___________165_________________ __________L__________________
___________Frank Sinatra_________________ ________171____________________
_______The Coffee Song_____________ _______165_______________________
14. Complete
the C code below by filling in the blanks to assign to the variable mycd
(declared before question #13) the following values: for the first track, name
= "I Drove All Night", length = 240.
/* assign "I Drove All Night" to the name
field of the first track of the variable named mycd */
_______strcpy(
mycd.tracks[0].name, "I Drove All Night");______
/*
assign 240 to the length field of the first
track of the variable named mycd */
________mycd.tracks[0].length = 240;______________________
15. Fill in the blanks below with the correct C code to dynamically allocate an array named albums (declared before question #13) that holds 1000 elements of data type Album.
albums = calloc(____1000________ , ____sizeof(Album)____);
16. Complete the code below to write your netid and lab section to the file named "cs101.dat". If you forget your lab section then use section "AYA".
#include <stdio.h>
void main(void)
{
char * netid = _______ "gambill" _____________;
char * section = _______"AYA" _____________;
FILE* fileptr = fopen(____"cs101.dat" _,___"w" ___);
fprintf(_fileptr_,"%s; %s",____netid____,___section__);
fclose(__fileptr_____);
}
17. Fill
in the blanks to complete the following C function named power
so that it compiles and runs without errors when placed in a complete C
program. The function power has two input
parameters x of data type double and n of
data type integer. The function power outputs xn
when n is a positive integer. For example, the following
statements,
printf("%lf \n", power(2.0,4)); /* displays 16 */
printf("%lf \n", power(3.0,3)); /* displays 27 */
would display 16 and 27 respectively if placed in a complete C program including the function named power shown below.
double power(double x, int n)
{
double temp;
if (n == _____1________) /* base case, Voom!!! */
return x;
else if (n % 2 == 0) /* n is even */
{
temp = power(x,n/2);
return temp * temp;
}
else /* n is odd */
return power(x,n-1) * __x___________________ ;
}
18. The program below compiles and executes without errors. Write the output of this program.
#include <stdio.h>
int f(int a, int b)
{
if (b == 1)
return a;
else
return a * f(a-1, b-1);
}
void main(void)
{
printf(" %i ", f(3,1));
printf(" %i ", f(3,2));
printf(" %i ", f(3,3));
}
________3_________ _______6__________ _______6__________