COP 3223 Section 3

Final Exam

Form A – Multiple Choice

Fall 2008

12/9/08

Lecturer: Arup Guha

Directions:This portion of the exam is closed book. Answer all multiple choice questions on the scantron. Each question has a single correct answer. In case of ambiguities, choose the most accurate answer. Each of these questions is worth 1 point for a correct answer. Incorrect answers and questions left blank are worth 0 points. There are a total of 50 possible points to earn on this section. You will have 75 minutes to finish this section. When you finish this section, double check that you have bubbled in your PID and Exam Version on the scantron and then hand in the scantron ONLY. Then you will receive the Free Response portion of the exam, which is out of 50 points. You may start on that section immediately, but may not use your textbook until all students have turned in their scantron.

1) Which of the following is the keyword that defines a constant in

ANSI C?

A)#define

B)int

C)double

D)final

E)None of the Above

2) Which of the following is NOT a reason to put comments in a

C program?

A)They make a program more readable.

B)They aid others reading the code understand it.

C)They can identify who wrote the code.

D)They make the program run faster.

E)None of the Above

3) Which of the following functions prints information to the screen?

A)print

B)printf

C)scanf

D)fwrite

E)None of the Above

4) Which of the following values can NOT be stored in an int?

A)0

B)35

C)-99999999

D)1000000000000

E)None of the Above

5) What type of error is forgetting a matching close parenthesis?

A)syntax error

B)run-time error

C)logical error

D)bus error

E)None of the Above

6) To what does the following expression evaluate in C?

3 + 4*(5 - 2) - 3/(1 + 1)

A)13.5

B)14

C)15

D)19.5

E)None of the Above

7) To what does the following expression evaluate in C?

(3 + 4*(6 - 5/4))%8

A)2

B)3

C)7

D)23

E)None of the Above

8) To what does the following expression evaluate in C?

19/(6+4) + 16/(3+2.0)

A)4

B)4.1

C)4.2

D)5.2

E)None of the Above

9) Which of the following expressions has a value closest to 4?

A)sqrt(pow(2,4))

B)pow(sqrt(4),4)

C)pow(2,pow(1,4))

D)pow(pow(1,4),2)

E)None of the Above

10) Which of the following lines of code definitively has a syntax error?

A)y = f(x);

B)y = 4(x + 3);

C)x = 17%y;

D)x += y;

E)None of the Above

11) What is the output of the following segment of code?

int x = 5, y = 3;

if (2*y > x)

printf("A");

else

printf("B");

if (x != y+2)

printf("C");

else

printf("D");

A)AC

B)AD

C)BC

D)CD

E)None of the Above

12) What is the output of the following segment of code?

int x = 3, y = 17;

if (y = 5*x)

printf("A");

else

printf("B");

if (y != 5*x)

printf("C");

else

printf("D");

A)AD

B)BC

C)BD

D)CD

E)None of the Above

13) What is the output of the following segment of code?

if (3 > 2)

if ( 7 != 7)

printf("A");

else

printf("B");

printf("C");

A)C

B)AC

C)BC

D)ABC

E)None of the Above

14) Which of the following boolean conditions has a chance to be

short-circuited to avoid a run-time error?

A)(a > 0 || b/a < 0)

B)(b/a > 0 & a != 0)

C)(b/a > 0 || a != 0)

D)(b/a < 0 & a == 0)

E)None of the Above

15) What is the output of the following code segment?

if (3 > 5);

printf("A");

printf("B");

if (6 > 4);

printf("C");

printf("D");

A)A

B)AC

C)ACD

D)ABCD

E)None of the Above

16) How many times will the code segment below print "hi"?

(Note: The answer is greater than 0.)

int i;

for (i=5; i<37; i+=3)

printf("hi!\n");

A)10

B)11

C)12

D)37

E)None of the Above

17) What is the value of sum at the end of the following code segment?

int sum = 0, i;

for (i=3; i<11; i++)

sum = sum + 2*i;

A)52

B)104

C)107

D)110

E)None of the Above

18) What is the value of sum at the end of the following code segment?

int sum = 0, i;

for (i=3; i<11; i++);

sum = sum + 2*i;

A)10

B)11

C)20

D)110

E)None of the Above

19) What is the output of the following code segment?

int n = 57;

while (n > 0) {

printf("%d", n%2);

n = n/2;

}

A)100100

B)100111

C)110101

D)111001

E)None of the Above

20) What is the value of sum at the end of the following code segment?

int sum=0, i=1;

do {

sum += 3*i;

i++;

} while (i < 0);

A)0

B)1

C)4

D)6

E)None of the Above

21) What is the value of sum at the end of the following code segment?

int sum=0, i;

for (i=1; i<11; i++) {

sum += i;

if (sum%7 == 0)

break;

}

A)1B)10C)28D)55E)None of the Above

22) What is the value of sum at the end of the following code segment?

int sum=0, i=1;

while (i < 10) {

if (i%3 == 0)

continue;

sum += i;

i++;

}

A)3B)27C)37D)45E)None of the Above

23) What does the following code segment do?

int i,j;

for (i=0; i<8; i++) {

for (j=0; j<8; j++)

if ((i+j)%2 == 0)

printf("X");

else

printf("O");

printf("\n");

}

A)Print an 8x8 patter of all X's.

B)Print an 8x8 pattern of all O's.

C)Print an 8x8 pattern with 4 rows of X's and 4 rows of O's

D)Print an 8x8 patter with 4 columns of X's and 4 columns of O's

E)None of the Above

24) The code below will be used for this question and the following

three questions.

#include <stdio.h>

int f(int a, int b);

int main() {

int a=3, b=5, c;

c = f(b,a);

printf("a = %d, b = %d\n", a, b);

a = f(a,c);

printf("a = %d, b = %d\n", a, b);

return 0;

}

int f(int a, int b) {

int c = 2*a + b;

a = (c+3)%17;

b = c - a;

printf("a = %d, b = %d\n",a,b);

return a+2*b;

}

What is the first line of output produced by this program?

A)a = 3, b = 5

B)a = 16, b = -3

C)a = -3, b = 16

D)a = 0, b = 13

E)None of the Above

25) What is the second line of output produced by this program?

A)a = 3, b = 5

B)a = 5, b = 3

C)a = 16, b = -3

D)a = -3, b = 16

E)None of the Above

26) What is the third line of output produced by this program?

A)a = 0, b = -3

B)a = 1, b = 18

C)a = 2, b = 14

D)a = 14, b = -3

E)None of the Above

27) What is the fourth line of output produced by this program?

A)a = 3, b = 5

B)a = 3, b = 30

C)a = 14, b = 5

D)a = 30, b = 5

E)None of the Above

28) The code below will be used for this question and the following

three questions.

#include <stdio.h>

int f(int *a, int *b);

int main() {

int a=3, b=5, c;

c = f(&b,&a);

printf("a = %d, b = %d\n", a, b);

a = f(&a,&c);

printf("a = %d, b = %d\n", a, b);

return 0;

}

int f(int *a, int *b) {

int c = 2*(*a) + (*b);

*a = (c+3)%17;

*b = c - (*a);

printf("a = %d, b = %d\n",*a,*b);

return (*a)+2*(*b);

}

What is the first line of output produced by this program?

A)a = 3, b = 5

B)a = 5, b = 3

C)a = 16, b = -3

D)a = 0, b = 13

E)None of the Above

29) What is the second line of output produced by this program?

A)a = 3, b = 5

B)a = 5, b = 3

C)a = 16, b = -3

D)a = -3, b = 16

E)None of the Above

30) What is the third line of output produced by this program?

A)a = 2, b = 14

B)a = 7, b = -3

C)a = 14, b = -3

D)a = 19, b = -3

E)None of the Above

31) What is the fourth line of output produced by this program?

A)a = 1, b = 5

B)a = 3, b = 5

C)a = 7, b = -3

D)a = 7, b = 5

E)None of the Above

32) The code below will be used for this question and the following

four questions.

#include <stdio.h>

int main() {

int arr[5], i, brr[5];

for (i=0; i<5; i++)

arr[i] = (3*i+2)%5;

for (i=0; i<5; i++)

brr[i] = arr[arr[i]];

for (i=0; i<5; i++)

printf("brr[%d]=%d\n",i,brr[i]);

return 0;

}

What is the first line of output produced by this program?

A)brr[0]=0

B)brr[0]=1

C)brr[0]=2

D)brr[0]=3

E)None of the Above

33) What is the second line of output produced by this program?

A)brr[1]=0

B)brr[1]=1

C)brr[1]=3

D)brr[1]=4

E)None of the Above

34) What is the third line of output produced by this program?

A)brr[2]=0

B)brr[2]=1

C)brr[2]=2

D)brr[2]=4

E)None of the Above

35) What is the fourth line of output produced by this program?

A)brr[3]=0

B)brr[3]=2

C)brr[3]=3

D)brr[3]=4

E)None of the Above

36) What is the fifth line of output produced by this program?

A)brr[4]=1

B)brr[4]=2

C)brr[4]=3

D)brr[4]=4

E)None of the Above

37) Which of the following functions reads from a file?

A)scanf

B)printf

C)fprintf

D)fileread

E)None of the Above

38) Which of the following lines correctly opens a file named

"file.txt" from which to read?

A)FILE* fout = open("file.txt", "write");

B)FILE* fout = fopen("file.txt", "write");

C)FILE* fout = open("file.txt", "w");

D)FILE* fout = "file.txt", "write";

E)None of the Above

39) Consider the following struct definition:

struct q {

int val;

char name[20];

int freq[10];

double price;

};

Assume that a variable named myStruct is declared as follows

struct q myStruct;

in main.

Which of the following would be syntactically valid in main

following the declaration of myStruct?

A)q.myStruct.val = 5;

B)myStruct.val = 5;

C)myStruct->val = 5;

D)myStruct[val] = 5;

E)None of the Above

40) Using the same struct and variable as in the previous question,

which of the following would be a syntactically correct

statement?

A)myStruct->freq[0] = 0;

B)myStruct.freq->0 = 0

C)myStruct.freq.0 = 0

D)myStruct->freq->0 = 0

E)None of the Above

41) Consider the following variable declaration based on the

previous two questions:

struct q Cues[20];

Which of the following statements is syntactically valid?

A)Cues.val[0] = 5;

B)Cues->val[0] = 5;

C)Cues[0].val = 5;

D)Cues[0].val[0] = 5;

E)None of the Above

42) Consider the following variable declaration based on the

previous three questions:

struct q *p;

p = (struct q)(malloc(sizeof(struct q)));

Which of the following statements is syntactically valid?

A)p.val = 5;

B)p->val = 5;

C)p[0]->val = 5;

D)p->val[0] = 5;

E)None of the Above

43) What is the maximum number of fields a struct can have?

A)1

B)2

C)3

D)more than 4

E)None of the Above

44) Which function dynamically allocates a node for a linked list in C?

A)new

B)malloc

C)get

D)fgets

E)None of the Above

45) If a pointer is pointing to no struct, to what should it be set?

A)15

B)NULL

C)ZERO

D)structNULL

E)None of the Above

46) Using the struct ll definition shown in class, which of the

following advances a pointer p to the next node in a linked

list, assuming that the next node exists?

A)p = p.next;

B)p = p->next;

C)p++;

D)p->next;

E)None of the Above

47) What is the following segment of code doing, (assuming the

struct ll definition given in class)?

struct ll* p;

p = (struct ll)(malloc(sizeof(struct ll)));

p->data = 3;

p->next = p;

A)Creating a linked list terminated by a NULL pointer.

B)Creating a node and adding it to a separate linked list.

C)Creating a linked list of one item that is circular.

D)Printing out the contents of a linked list.

E)None of the Above

48) Which C function below can be used to compare two strings in

alphabetical order?

A)equals

B)strcmp

C)strlen

D)strcpy

E)None of the Above

49) Which character is the last stored in a C string?

A)'\0'

B)'\t'

C)'\n'

D)'\b'

E)None of the Above

50) Who is the central character in Mark Twain's classic, "The Adventures of Huckleberry Finn?"

A)Huckleberry Finn

B)Mark Twain

C)Sherlock Holmes

D)Britney Spears

E)Abraham Lincoln

1) (10 pts) Write a function that takes in a string and reverses its contents. (Hint: It may be useful to use the string function strlen.)

void reverse(char word[]) {

}

2) (10 pts)Write a function that prints out the second, fourth, sixth, etc. items in the linked list pointed to by first. Make sure to take care of any possible NULL pointer errors. If a list has fewer than two nodes, nothing should be printed.

struct ll {

int data;

struct ll *next;

};

void printEven(struct ll *first) {

}

3) (10 pts) Write a function that takes in an integer array called values that has length items in it and returns the number of times searchVal is stored in the array.

int inArray(int values[], int length, int searchVal) {

}

4) (10 pts) An infinite series related to PI is the following:

Write a function that takes in a single positive integer n, representing the number of terms in this series to compute and returns the corresponding approximation of π. (e.g if n=1, then the function should return 4. If n=2, the function should return 2.66666667.)

double piApprox(int n) {

}

5) (10 pts) After taking COP 3223, you get a job as a TA for the course. The first task your instructor gives you is to write a program to automate calculating of grades. In particular, each student takes 5 quizzes worth 2% each of the total grade and 3 exam that are each worth 30% of the total grade. Quizzes are out of 10 points and exams are out of 100 points. Your instructor has already built a struct to store all the raw scores for one student:

struct Cstudent {

char name[20];

int quizzes[5];

int exams[3];

};

As you might imagine, inside of a variable of this struct, quizzes[0] is the first quiz grade (out of 10), quizzes[1] is the second quiz grade, etc. and exams[0] is the first exam grade (out of 100), exams[1] is the second exam grade and exams[2] is the third exam grade. Write a function that takes in a variable of type struct Cstudent (by reference) and returns a double representing the student's average percentage in the course (in between 0 and 100, inclusive). You may assume that all array elements in the struct already store the proper quiz and exam grades.

double calcScore(const struct Cstudent *johndoe) {

}