COP 3223 Section 2 Exam #2

COP 3223 Section 2 Exam #2

COP 3223 Section 2 Exam #2

Form A

Fall 2008

11/6/08

Lecturer: Arup Guha

Directions: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 2 points for a correct answer. Incorrect answers and questions left blank are worth 0 points. Hand in ONLY the scantron, keep the test questions, and take the free response form.

1) What is the return type of the function main in ANSI C?

A)void

B)int

C)float

D)double

E)None of the Above

2) Consider writing a minimum function that returns the minimum of

its three integer parameters. Part of the function is written

below. What line of code should replace the blank below so that the

function works properly?

int minimum(int a, int b, int c) {

______

return a;

else if (b < c)

return b;

return c;

}

A)if (a > b > c)

B)if (a < b)

C)if (a < b & b < c)

D)if (a < b & a < c)

E)None of the Above

3) Which of the following is a valid prototype for the minimum function in the previous question?

A)int minimum(int,int,int);

B)int minimum(a, b, c);

C)minimum(int a, int b);

D)int minimum(int* a, int* b, int* c);

E)None of the Above

4) Which of the following may be a reason to utilize a pass by reference parameter?

A)To avoid using pointers.

B)To get to use the % sign more.

C)To allow a change the function to change the value of a variable in another function.

D)To make the program less error-prone.

E)None of the Above

The next four questions(5-8) refer to the following program below:

#include <stdio.h>

int f(int a, int b);

int main() {

int a=4, b=7;

if (f(b,a))

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

else

printf("Zero is the answer.\n");

b = f(3*a+b, 3*b-a);

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

return 0;

}

int f(int a, int b) {

int c;

c = 2*a%b;

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

b = c - (2*b%a);

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

return c;

}

5) What is the first line of output of the program above?

A)a = 3, b = 0

B)a = 3, b = 4

C)a = 4, b = 7

D)a = 14, b= -2

E)None of the Above

6) What is the second line of output from the program above?

A)a = 3, b = -1

B)a = 4, b = 7

C)a = 14, b = -2

D)a = 22, b = 10

E)None of the Above

7) What is the third line of output from the program above?

A)a = 4, b = 7

B)a = 17, b = 19

C)a = 19, b = -11

D)a = 19, b = 17

E)None of the Above

8) What is the fourth line of output from the program on the previous page?

A)a = 4, b = 0

B)a = 4, b = 4

C)a = 10, b = 4

D)a = 19, b = -11

E)None of the Above

The next four questions(9-12) refer to the following program below:

#include <stdio.h>

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

int main() {

int a=9, b=11;

if (f(&b,&a))

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

else

printf("Zero is the answer.\n");

b = f(&a, &b);

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

return 0;

}

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

int c;

c = 2*(*a)%(*b);

(*a) = c + (3*(*b)%(*a));

(*b) = c - (2*(*b)%(*a));

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

return c;

}

9) What is the first line of output for the program above?

A)a = 9, b = -3

B)a = 9, b = -2

C)a = 9, b = 4

D)a = 13, b = -3

E)None of the Above

10) What is the second line of output of the program above?

A)a = 9, b = 4

B)a = 9, b = 11

C)a = 11, b = 9

D)a = 13, b = -2

E)None of the Above

11) What is the third line of output of the program on the previous page?

A)a = 1, b = 11

B)a = 8, b = 11

C)a = 11, b = 1

D)a = 11, b = 8

E)None of the Above

12) What is the fourth line of output of the program on the previous page?

A)a = 1, b = 11

B)a = 4, b = 9

C)a = 8, b = 11

D)a = 11, b = 8

E)None of the Above

13) Which of the following declares an integer array of size 20?

A)int[] values = new int[20];

B)int values;

C)int[20] values;

D)int values[20];

E)None of the Above

14) Which of the following initializes each entry in the array values to 100? (Note: Assume values is an integer array of size 20 and i is already declared as an integer.)

A)values = 100;

B)values[20] = 100;

C)values[0..19] = 100;

D)for (i=0; i<20; i++) values[i] = 100;

E)None of the Above

15) Which of the following variable declarations would be most appropriate to use in an implementation of tic-tac-toe?

A)double board[3][3];

B)char board[3][3];

C)char board[];

D)int board[33];

E)None of the Above

The next four questions(16-19) will involve the code segment below:

int a[5];

int i;

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

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

int t = a[0];

a[0] = a[1] + a[2];

a[3] = a[3] + a[4];

a[4] = t;

printf("a[0] = %d\n", a[0]);

printf("a[1] = %d\n", a[1]);

printf("a[3] = %d\n", a[3]);

printf("a[4] = %d\n", a[4]);

16) What is the first line of output created by this code segment?

A)a[0] = 0;

B)a[0] = 1;

C)a[0] = 2;

D)a[0] = 3;

E)None of the Above

17) What is the second line of output created by this code segment?

A)a[1] = 0;

B)a[1] = 1;

C)a[1] = 2;

D)a[1] = 3;

E)None of the Above

18) What is the third line of output created by this code segment?

A)a[3] = 1;

B)a[3] = 2;

C)a[3] = 3;

D)a[3] = 4;

E)None of the Above

19) What is the fourth line of output created by this code segment?

A)a[4] = 1;

B)a[4] = 2;

C)a[4] = 3;

D)a[4] = 4;

E)None of the Above

20) After winning Tuesday's election, Senator Obama has to start thinking about moving into the White House. Which color of the ones below is closest to the color of the outside of his new residence?

A) white

B) brown

C) blue

D) purple

E) black

Fall 2008 COP 3223 Section 3

Exam 2 Answer Sheet

Last Name: ______, First Name: ______

1) (20 pts) The mode in a list of numbers is the most frequently occurring number. Complete the program below so that it calculates the mode of the test scores entered by the user. Declare extra variables as needed. Note: All test scores will be in between 0 and 100, inclusive. At most, 1000 test scores will be entered.

#include <stdio.h>

int main(void) {

int numScores, mode;

int freq[101];

printf("How many scores are you entering?\n");

scanf("%d", &numScores);

printf("The most common score was %d.\n", mode);

return 0;

}

2) (10 pts) Fill in the function below that it returns the harmonic mean of two positive numbers. The harmonic mean of two numbers x and y is defined as .

double harmonicMean(double x, double y) {

}

3) (15 pts) An inversion in an integer array is a pair of integers that are "out of order." Specifically, for a pair, if the larger number appears in the array BEFORE the smaller number, it is an inverted pair. For example, consider an array of size five storing the following values: 6, 1, 8, 2, 3. The inverted pairs are (6, 1), (6, 2), (6, 3), (8, 2) and (8, 3). Thus, this example array has 5 inversions. Write a function that calculates the number of inversions in an integer array. The function will have two parameters: the array and its length.

int numInversions(int values[], int length) {

}

4) (15 pts) Write a function which takes in a single positive integer and returns 1 if the number is prime and 0 otherwise. Note: 1 is NOT a prime number. Otherwise, a prime number is any positive integer that does NOT have any factors greater than 1 other than itself.

int isPrime(int n) {

}