








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!









Fall 2006 Page 1 of 14
Failure to read and follow the instructions below may result in severe penalties. Failure to adhere to these directions will not constitute an excuse or defense.
Name (print: Last name, First)
VT PID: (print: campus email address)
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
Fall 2006 Page 2 of 14
Given the following code fragment:
int sum = 0; for (int i=1; i < 4; i++) { sum = sum + i; } cout << "sum = " << sum << endl;
1) What is wrong with the code fragment above which is intended to add the numbers from 1 to 4 together and print out the result which would be 10?
Answer : 5
For loop should include i = 4. The loop should be for (int i=1; i < = 4; i++)
For the next 2 questions, consider execution of the following code fragment (assume any additional declarations, etc, needed to make the code syntactically correct):
int x = 1, y = 1; while (y < 100) { y = y * x; x++; cout << "y = " << y << endl; }
2) How many values of y will the above code display, (i.e how many lines of output will it produce)?
*Answer: 5 Y is the product of 1×2×…×x. Immediately after the statement of y = y x, the x and y pair could be 1 1 2 2 3 6 4 24 5 120
When y = 120, the while loop terminates, and x = 6
Fall 2006 Page 4 of 14
For the next 4 questions, consider the following incomplete function (assume any additional declarations, etc, needed to make the code syntactically correct):
//sum the rational series: 1/2 + 1/3 + 1/4 + 1/5 +... + 1/n void harmonicSum( ______ n, // # ______ numerator, // # ______ denomerator) // same as # { int num = 1, den = 2, frac = 2; for (frac=3; frac <= n; frac++) { // hint 1/2 + 1/3 == 5/6; 5/6 + 1/4 == 26/24 etc... num = _________________; // #6 set the numerator den = _________________; // #7 set the denomerator } numerator = num; denomerator = den; } For this code segment, variables numerator and denomerator are called by reference. They are going to bring the value to the calling function as function return type is void.
From for loop and prior conditions, we know that variable num represents the current numerator, and variable den represents the current denomerater, and 1/frac is the next value we are going to add into the summation.
So basically we are asked what is the numerator and denomerator given the following formula:
num/den + 1/fac
Then den = den * fac num = num * fac + den Answer for 4: 1 Answer for 5: 2 Answer for 6: 5 Answer for 7: 2
4) Which of the following expressions should replace the blanks in line #4 above to correctly set the best parameter passing mechanism for the argument n?
5) Which of the following expressions should replace the blanks in line #5 above to correctly set the best parameter passing mechanism for the argument numerator? Hint – it is the same mechanism that must be used for the denomerator parameter.
6) Which of the following expressions should replace the blanks in line #6 above to correctly compute the numerator?
Fall 2006 Page 5 of 14
7) Which of the following expressions should replace the blanks in line #7 above to correctly compute the denomerator?
The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13 … Any term (value) of the sequence that follows the first two terms (0 & 1) is equal to the sum of the preceding two terms. For the next 2 questions, consider the following incomplete method to compute any term of the Fibonacci sequence, (assume any additional declarations, etc, needed to make the code syntactically correct):
int fibonacci( int term ) { int fib1=0, fib2 = 1, fibn;
if (term == 1) return fib1; if (term == 2) return fib2;
for ( ____________________________ ) // #8 loop to the nth term { fibn = ____________; // #9 compute the next term fib1 = ____________; // #10 reset the second preceding term fib2 = ____________; // #11 reset the immediate preceding term }
return( ___________ ); // #12 return the computed term }
8) Which of the following expressions should replace the blanks in line #8 above to correctly setup the loop?
Answer : 3 Function returns before the for loop for term == 1 || 2. For the for loop condition, we have make sure it starts from term ==3.
Fall 2006 Page 7 of 14
For the next 2 questions, trace the execution of the following code:
int test[10] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
int i, x=0; for ( i=0; i < 10; i++) { if ( test[i] % 2 == 0 ) { x = x + test[i]; } }
13) What is the value of i after this code has executed?
1 2) 2 3) 0 4) 11
21 6) 34 7) 9 8) 10 9) none of these
When i is in 1 to 9 (less than 10), the for loop continues. Remember after the execution of loop body, i is incremented by 1. If currently i == 9, after the iteration i == 10, and for loop ends.
Answer : 8
14) What is the value of x after this code has executed?
18 2) 23 3) 27 4) 44
52 6) 63 7) 88 8) 0 9) none of these
Answer: 4 X is the summation of even numbers in the array, that is 2 + 8 + 34 = 44
Fall 2006 Page 8 of 14
For the next 5 questions, consider this (incomplete) code segment. The function maxVector( ) is intended to take as a parameter a two dimensional array of int values (matrix) and return the index of the row that stores the largest vector (row) sum in the matrix.
const int NUMROWS = 100; const int NUMCOLS = 100;
// maxVector() takes an integer matrix & returns the // index of the largest vector in the matrix. // If the matrix does not contain any values // then -1 is returned. // Parameters: matrix 2-dim array of ints // rows number of vectors stored in the matrix // Returns: index of row with largest sum // int maxVector( __________ , // line A ___________) { // line B
int sum = 0, maxVec= -1, maxSum = INT_MIN;
for (int r=0; r < _____________; r++) // line C { sum = 0;
for (int c=0; (c < _____________); c++) // line D { sum = sum + _____________; // line E } if (sum > maxSum) { maxSum = sum; maxVec = r; } }
return maxVec; }
15) How should the blank for the matrix parameter in line A be filled?
Answer: 3 When array is used as formal parameter, the capacity of the first dimension is left blank, but we need to fill out the capacity for the second dimension.
16) How should the blank for the rows parameter in line B be filled?
Answer: 2
Fall 2006 Page 10 of 14
18) How should the blank in line D be filled?
Answer: 3
19) How should the blank in line E be filled?
Answer: 2
For the next 2 questions consider the following code:
void main () { int x0 = 3; int y1 = 4;
mystery (x0, y1); cout << x0 << ‘ ’ << y1 << endl; }
void mystery (int p0, int& q1) { p0 = p0 + 1; q1 = q1 + 2; }
20) What is printed out for x0?
Answer: 3 X0 is call by value. The function call inside main function doesn’t change its value
21) What is printed out for y1?
Answer: 6 Y1 is call by reference. The function call inside main function changes its value. Before the function call, y == 4. Inside of the called function, its value is incremented by 2, which make y1 6.
Consider the following code:
int main() { const int MAX = 100; int x[MAX];
for (int i = MAX; // line 1
Fall 2005 Page 11 of 14
i >= 0; // line 2 i--) // line 3 x[i] = MAX - i; // line 4
return 0; // line 5 }
22) Which line of code should be changed to prevent a possible runtime error in the program while retaining the identical functionality, (i.e. purpose), of the code?
Answer: 1 The loop operates for array index I between [ Max, 0]. We know the index of array should be between [MAX-1, 0].
Consider the following code:
void argh (int a); int a, b; // file scope int main() { // try this a=5; b=7; argh(b); cout << a << ‘ ’ << b << endl; return 0; }
void argh (int a) { a = a / 2; b = a - 2; }
23) What are the values of variables a and b output by the code above?
Answer: 3
Here a and b are global variables. Whenever the identifiers are not redefined, the references are to the global variable a and b.
If argh() is call by value, then the reference of a inside argh() is binding to the declaration in the function heading. Here local variable a == 7/ ==3 inside argh(). Inside argh(), b is not redeclared, then the reference is binding to the global variable b. Its value is changed to a – 2 == 1
24) What would be the values of variables a and b output by the code above if the prototype and header for the function argh was changed to argh(int& a)?
Fall 2005 Page 13 of 14
For the next 4 questions, consider the incomplete function definition given below:
// range takes an array of integers and count the number of elements // in the array falls within the given range, inclusively. // // Parameters: // Array[] array containing integers. // usage number of array locations holding values // lowRange starting value of the desired range // hiRange ending value of the desired range
int range( int _________, int usage, int lowRange, int hiRange)// Line A { int LoopCounter, inRange=0;
for(LoopCounter=0;________;LoopCounter++) //Line B { if ( ( _________ >= lowRange) && //Line C ( _________ <= hiRange) ) //Line D inRange++ }
return( inRange ); }
27) How should the blank for the first parameter in Line A be filled?
Answer : 2
When one dimensional Array as formal parameters, the capacity of array is left blank.
28) How should the blank in Line B be filled?
Answer: 1 Array index from o to usage -
29) How should the blank in Line C be filled?
Answer: 2 Reference of Array element: Array[index]
30) How should the blank in Line D be filled?
Fall 2005 Page 14 of 14
Answer: 2 Reference of Array element: Array[index]