






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Midterm # May 5, 2001 Problem numbering may differ from the test as given.
All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers suggests otherwise.
Pick the true statement.
A. "Functional decomposition" is a technique for designing a program. B. "Functional decomposition" refers to the natural decay of a program over its lifetime. C. A "static call graph" shows all the internal detail of the functions in a program. D. One reason symbolic constants (#define symbols) are desirable is because they don’t have to conform to the same naming rules as other identifiers.
E. Comments describing how functions work allow the compiler to double-check your function. ANSWER: A
What is printed by the following code? (The variables are integers).
#define TRUE 1 #define FALSE 0 ... a = 1; b = 6; c = 5; status = ((c / b) || (a * b)) && ((c - b / a) * c); printf("%d", status);
Widely missed. A surprising number of students chose D or E.
Look at the following code, and determine what value is printed.
int trace1=0, trace2=0;
for (j=0; j < 4; j = j+1) { for (k=0; k < 2; k = k+1) { trace1++; } trace2++; } printf ("%d", trace1+trace2); ...
Missed by quite a few people. A conservative way to solve it is to make four columns, one for each variable, and carefully trace each step. Start out with:
j k trace1 trace ?? 0 0 0? 0 0 0 0 0 0 0 0 1 0 etc. Sometimes you will discover a feature or general pattern which lets you skip ahead to the answer.
What is the ouput of the following code? Assume all variables are integers.
if ((a > 5) && (a <= 5)) printf("Sonics\n"); if ( !( (b == 2) || (b != 2) ) ) printf("Mariners\n"); else printf("Seahawks\n");
A. Mariners B. Seahawks C. Mariners Seahawks D. Sonics Mariners Seahawks
E. unable to determine: depends on the values of a and b ANSWER: B
Study this program, and determine: What would the printf in main produce?
void f1 (int i, int j) { i = i + 1; j = j - 1; }
int f2 (int i, int j) { i = i + 1; j = j - 1; return (i + 2); }
int main (void) { int i, j; i = 4; j = 5; f1(j, i); j = f2(i, j); i = f2(j, i); f1(i, j); printf("i is %d, j is %d\n", i, j); return 0; }
A. i is 4, j is 10 B. i is 7, j is 5 C. i is 10, j is 7 D. i is 5, j is 7
E. i is 4, j is 5 ANSWER: C
What would the following program fragment output?
int main (void) { int i, j = 1; for (i = 3; i > 1; i = i - 1) { j = j + 5 * i; }
printf("%d ", j); }
E. The loop will not stop running. ANSWER: C
Which call graph is best to describe the following algorithm that helps schedule transportation pick-ups: The main program asks the user for the position they want to be picked up at, and then computes the cost. When asking for the position, the program will check to see if the position is in the bounded area. As part of computing the cost, the program asks users for type of transaction (bus, cargo) and uses an appropriate function to compute the cost.
[printf/scanf are omitted from the call graphs for this problem.]
main
get_position computer_cost check_bounded_area bus_cost cargo_cost B. main /
get_position compute_cost | /
check_bounded_area bus_cost cargo_cost C. main /
compute_cost get_position | /
check_bounded_area bus_cost cargo_cost D. main | get_position /
check_bounded_area compute_cost /
bus_cost cargo_cost
E. None of the above choices even remotely describes the given algorithm. ANSWER: B
Given the function definition below, what does the following statement print out?
switchAndPrint(2);
void switchAndPrint(int x) { switch(x) { case 1: printf("1"); break; case 2: printf("2"); default: printf("3"); break; } }
Study the following function testForDivisors, which is given without comments, but which does in fact have something to do with the divisors of an integer.
What value is returned by this function when it is called with an argument value of 14?
int testForDivisors(int x) { int nD = 0; int testNumber;
for(testNumber = 2; testNumber < x; testNumber++) { if(x % testNumber == 0) nD++; } return nD; }
Which C condition captures the meaning of the English-language phrase "P or Q but not both"?
Based on the Question of the Day 4/01/01. A good way to verify the answer is with a truth table. A = "P or Q but not both" means this:
Examine this code fragment, and determine the value of rain[0] after the last statement shown.
int rain[4]; /*declares an array of 4 integers. The elements are rain[0], rain[1], rain[2], and rain[3] */ int i;
for (i=0; i < 3; i=i+1) { rain[i] = 1; } rain[0] = rain[1] + rain[2]; /* What is rain[0] now?? */
Your task: write a function which, given any two integers, determines whether they constitute a valid pair.
You may not call any functions at all (no, not printf and not scanf!), except the following one:
int isPrime (int num); /returns 1 if num is prime, 0 otherwise/
You are not asked to implement isPrime.
Part of your task is to determine appropriate parameters and return type (if any) of the function. Include comments to describe what these are (but you can assume the reader of your code is familiar with the definition of "valid pair"). Use your best style.
Some examples of valid or invalid pairs: 15 17 is not valid, because 15 is not prime. 13 19 is not valid, because there is a prime (17) between 13 and 19. 17 19 is valid. 31 29 is valid 31 31 is not valid (the numbers must be different)
Start your code here and continue on the next page if necessary.
/************************* sample solution ******************/
int isValidPair(int num1, int num2) { /*If num1 and num2 constitute a "valid pair", the function returns 1. Otherwise, the function returns 0. */
int currentNum; /* will be used in the loop/ int temp; /used for swapping*/
if (num1 == num2) return 0; if (!(isPrime(num1))) return 0; if (!(isPrime(num2))) return 0;
if (num1 > num2) { /the input numbers are out of order -- swap them/ temp = num1; num1 = num2; num2 = temp; }
for (currentNum = num1+1; /don’t check num1 !!/ currentNum < num2; /dont’ check num2, either!/ currentNum++) {