COP 3223 Section 2 Exam #1

Total Page:16

File Type:pdf, Size:1020Kb

COP 3223 Section 2 Exam #1

COP 3223 Section 3 Exam #2 Form A Fall 2007 10/23/07

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 3 points for a correct answer, except for question 17, which is worth 2 points. Incorrect answers and questions left blank are worth 0 points. Hand in ONLY the scantron, keep the test questions, and take the free response section. Please reference the following program to answer questions #1 and 2.

#include void f(int a, int b); int main() { int a=3, b=5; f(b+a,b-a); printf("a=%d, b=%d\n",a,b); return 0; } void f(int a, int b) { int tmp = a+b; a = 2*tmp; b = a - b; printf("a=%d, b=%d\n",a,b); }

1) What is the first line of output produced by the program above?

A)a=3, b=5 B)a=8, b=2 C)a=18, b=20 D)a=20, b=18 E)None of the Above

2) What is the second line of output produced by the program above?

A)a=3, b=5 B)a=5, b=3 C)a=8, b=2 D)a=2, b=8 E)None of the Above

3) How many times does the loop below execute? int i; for (i=3; i<=93; i=i+6) printf("one more time\n");

A)3 B)15 C)16 D)90 E)None of the Above

4) How many times does the word KNIGHTS get printed out in the code segment below? int i; for (i=0; i<40; i++) { printf("KNIGHTS\n"); if ((i+1)%12 == 0) break; }

A)11 B) 12 C) 13 D) 39 E)None of the Above

5) Which of the following functions reads in a single character from the keyboard?

A)getchar B)putchar C)printf D)scanf E)None of the Above Please reference the following program to answer questions #6, 7 and 8.

#include void g(int *a, int b); int main() { int a=3, b=5; g(&b,a); printf("a=%d\n",a); printf("b=%d\n",b); return 0; } void g(int *a, int b) { (*a) = (*a) + 2*b; b = (*a) - b; printf("a=%d, b=%d\n",*a,b); }

6) What is the first line of output produced by the program above?

A)a=5, b=3 B)a=3, b=5 C)a=8, b=11 D)a=11, b=8 E)None of the Above

7) What is the second line of output produced by the program above?

A)a=3 B)a=5 C)a=8 D)a=11 E)None of the Above

8) What is the third line of output produced by the program above?

A)b=3 B)b=5 C)b=8 D)b=11 E)None of the Above

9)How many times does the code segment below print out the letter X? int i,j; for (i=0; i<10; i++) for (j=1; j<=12; j+=2) printf("X");

A)10 B)12 C)22 D)120 E)None of the Above

10) How many times does the code segment below print out the word KNIGHTS? int i; for (i=0; i<40; i++) { if ((i+1)%12 == 0) continue; printf("KNIGHTS\n"); }

A)36 B)37 C)39 D)40 E)None of the Above Please reference the following program to answer questions #11 and 12.

#include int h(int a, int b); int main() { int a=3, b=5; b = h(b,a); printf("a=%d, b=%d\n",a,b); return 0; } int h(int a, int b) { a = a*b%8; b = b + a; printf("a=%d, b=%d\n",a,b); return 2*a - b; }

11) What is the first line of output produced by the program above?

A)a=7, b=5 B)a=5, b=7 C)a=7, b=10 D)a=10, b=7 E)None of the Above

12) What is the second line of output produced by the program above?

A)a=3, b=4 B)a=3, b=5 C)a=4, b=3 D)a=5, b=3 E)None of the Above

13) If the ascii value of 'A' is 65, what is the integer value of the expression 'C' + 'F' – 'D'?

A)65 B)67 C)69 D)71 E)None of the Above

14) Which of the following declares a file pointer and opens an input file called "data.txt" from which to read?

A)FILE *ifp = fopen(data.txt,r); B)FILE *ifp = fopen(data.txt,"r"); C)FILE *ofp = fopen("data.txt", "r"); D)FILE *ifp = fopen("data.txt", "w"); E)None of the Above

15) Which of the following writes the word HELLO to the output file pointed to by the file pointer ofp?

A)printf("HELLO"); B)fprintf("HELLO"); C)fprintf("HELLO","w"); D)fprintf(ofp,"HELLO"); E)None of the Above

16) What is the return value of the expression isupper('C')?

A)0 B)1 C)2 D)3 E)None of the Above

17) In which city do the American League Champion Boston Red Sox play their home games?

A)Boston B)Plymouth C)Redding D)New York E)None of the Above Fall 2007 COP 3223 Section 3 Exam 2 Free Response Answer Sheet Last Name: ______, First Name: ______

1) (5 pts) Convert the following segment of code that uses a while loop into an equivalent segment of code that uses a for loop. (Note: Assume that all variables used in the segment of code have been previously declared and initialized.) cnt = 0; while (cnt < n) { sum = sum + cnt; cnt = cnt+1; }

2xy 2) (15 pts) The harmonic mean of two positive real numbers x and y is equal to . x  y Write a function that takes in two parameters x and y, and returns their harmonic mean. If either of x and y is 0 or negative, return 0, otherwise return the value described above. double HarmonicMean(double x, double y) {

} 3) (10 pts) Complete the function below which "matures" your account by one year. In particular, the function will take in two pointers: a pointer to a integer which stores your age, and a pointer to a double, which stores the amount of money in your bank account. The task of the function is to update your age (by adding one to it), and update the amount in your bank account by adding 5% of its value to it. void matureOneYear(int *pAge, double *pMoney) {

}

4) (20 pts) A file named nba.txt stores the scoring averages of all NBA players. The format of the file is as follows. The first number in the file, n, is a positive integer representing the number of players in the file. The following n lines contain two numbers each (separated by spaces). The first number is a unique player identification number, which is always an integer. The second number is that player's scoring average, which is a real number. Complete the program below so that it reads in the information from the file and prints out both the player identification number and scoring average of the player with the highest scoring average. (Note: you are guaranteed that only one player will have the highest scoring average.)

#include int main() {

FILE *ifp;

ifp = fopen("nba.txt", "r");

fclose(ifp); return 0; }

Recommended publications