Docsity

C++ Code Analysis: Identifying Errors and Understanding Function Behavior - Prof. Suqin Ge, Exams of Computer Science

Toggle sidebar
bg1
CS 1044 Programming in C++ Test 2 Form A
Fall 2006 Page 1 of 14
READ THIS NOW!
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.
• Print your name and the VT email PID in the spaces provided below.
• Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form.
Code Form A on the Opscan.
• Choose the single best answer for each question — some answers may be partially correct. If you mark more
than one answer, it will be counted wrong.
• Unless a question involves determining whether given C++ code is syntactically correct, assume that it is. The
given code has been compiled and tested, except where there are deliberate errors. Unless a question
specifically deals with compiler #include directives, you should assume the necessary header files have been
included.
• Note that questions about printed values disregard formatting completely.
• Be careful to distinguish integer values from floating point (real) values (containing a decimal point). In
questions/answers which require a distinction between integer and real values, integers will be represented
without a decimal point, whereas real values will have a decimal point, [1044 (integer), 1044.0 (real)].
• When you have completed the test, sign the pledge at the bottom and turn in the test and your Opscan.
• This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used
during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this
examination with any student who has not taken it. You must return this test form when you complete the
examination. Failure to adhere to any of these restrictions is an Honor Code violation.
• There are 30 multiple-choice questions.
• The answers you mark on the Opscan form will be considered your official answers.
Do not start the test until instructed to do so!
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.
signature
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download C++ Code Analysis: Identifying Errors and Understanding Function Behavior - Prof. Suqin Ge and more Exams Computer Science in PDF only on Docsity!

Fall 2006 Page 1 of 14

READ THIS NOW!

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.

  • Print your name and the VT email PID in the spaces provided below.
  • Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form. Code Form A on the Opscan.
  • Choose the single best answer for each question — some answers may be partially correct. If you mark more than one answer, it will be counted wrong.
  • Unless a question involves determining whether given C++ code is syntactically correct, assume that it is. The given code has been compiled and tested, except where there are deliberate errors. Unless a question specifically deals with compiler #include directives, you should assume the necessary header files have been included.
  • Note that questions about printed values disregard formatting completely.
  • Be careful to distinguish integer values from floating point (real) values (containing a decimal point). In questions/answers which require a distinction between integer and real values, integers will be represented without a decimal point, whereas real values will have a decimal point, [1044 (integer), 1044.0 (real)].
  • When you have completed the test, sign the pledge at the bottom and turn in the test and your Opscan.
  • This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this examination with any student who has not taken it. You must return this test form when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code violation.
  • There are 30 multiple-choice questions.
  • The answers you mark on the Opscan form will be considered your official answers.

Do not start the test until instructed to do so!

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.

signature

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?

  1. the loop control variable i must be declared prior to the loop.
  2. the sum variable must be initialized to one.
  3. loop control variable i must be initialized to zero.
  4. the test condition must be ==
  5. the test condition must be <=
  6. none of the above, (i.e. the code is correct)

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)?

  1. 6 7) 7 8) 100 9) none of the above

*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?

  1. int 2) int& 3) int[]
  2. const& int 5) int const& 6) none of these

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.

  1. int 2) int& 3) int[]
  2. const& int 5) int const& 6) none of these

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

  1. num + frac
  2. (num + frac ) + den
  3. (num + frac ) * den
  4. (num * frac ) * den
  5. (num * frac ) + den
  6. none of these

7) Which of the following expressions should replace the blanks in line #7 above to correctly compute the denomerator?

  1. den + frac
  2. den * frac
  3. (num / frac ) * den
  4. (num / den ) + frac
  5. (num / den ) * frac
  6. none of these

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?

  1. int n=0; n < term; n++ 2) int n=1; n < term; n++
  2. int n=2; n < term; n++ 4) int n=3; n < term; n++
  3. int n=4; n < term; n++ 6) none of these

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. 1 2) 2 3) 0 4) 11

  2. 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?

  1. 18 2) 23 3) 27 4) 44

  2. 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?

  1. int matrix[][] 2) int[][] matrix
  2. int matrix[][NUMCOLS] 4) int[][NUMCOLS] matrix
  3. it should be left blank 6) none of these

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?

  1. int& rows 2) const int& rows
  2. int[] rows 4) const& int rows
  3. it should be left blank 6) none of these

Answer: 2

Fall 2006 Page 10 of 14

18) How should the blank in line D be filled?

  1. NUMROWS 2 ) rows
  2. NUMCOLS 4) cols
  3. none of these

Answer: 3

19) How should the blank in line E be filled?

  1. matrix[r,c] 2) matrix[r][c]
  2. matrix[c,r] 4) matrix[c][r]
  3. none of these

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?

  1. 1 2) 2 3) 3 4) 4 5) 5
  2. 6 7) 0 8) none of the above

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?

  1. 6 7) 0 8) none of the above

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?

  1. line 1 2) line 2 3) line 3
  2. line 4 5) line 5 6) no line should be changed

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?

  1. & Array 2) Array[] 3) Array
  2. []Array 5) & Array[] 6) none of these

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?

  1. LoopCounter < usage 2) LoopCounter < lowRange
  2. LoopCounter < hiRange 4) LoopCounter < inRange
  3. none of these

Answer: 1 Array index from o to usage -

29) How should the blank in Line C be filled?

  1. Array[usage] 2) Array[LoopCounter]
  2. Array[inRange] 4) Array[lowRange]
  3. Array[hiRange] 6 ) none of these

Answer: 2 Reference of Array element: Array[index]

30) How should the blank in Line D be filled?

Fall 2005 Page 14 of 14

  1. Array[usage] 2) Array[LoopCounter]
  2. Array[inRange] 4) Array[lowRange]
  3. Array[hiRange] 6 ) none of these

Answer: 2 Reference of Array element: Array[index]