COMP 1572 Winter 2002 Solutions to Exam 1 1. Recall that the increment statement is executed on each iteration after the loop body and before the continuation test. (See page 21 of Wang.) Since the result of the increment expression is not stored or tested, it doesn't matter whether it is a pre-increment or a post-increment. All of the choices x++ or x+=1 or x++ increment x, and so they have the same effect. The correct answer is (d), all of the above 2. The correct answer is (c) - the definition of a function will not be visible to other functions if the function definition is in another source code file. All the other functions need is the interface, which they can get from a function prototype. 3. The correct answer is (a) 4. You may not omit a return type from a function definition (define the function is void), you may not return a value from a function declared as void, you may not declare a function parameter again inside a function. The correct answer is (d), if you so choose, you may use the same names for arguments passed to a function and for the parameters in the function itself. 5. The shifting value is a, the scaling value (the width of the desired range of integers) is b. The correct answer is (c) 6. To generate random numbers, srand needs to be called only once, before you begin generating the numbers themselves with rand(). This initializes, or "seeds", the random number generator. You can use time as a seed value. The correct answer is (d). 7. f(0) = 0, f(1) = 1, f(2) = f(0)+f(1) = 1, f(3) = f(2) + f(1) = 1 + 1 = 2 f(4) = f(3) + f(2) = 2 + 1 = 3 f(5) = f(4) + f(3) = 3 + 2 = 5 f(6) = f(5) + f(4) = 5 + 3 = 8 The correct answer is (d) 8. The caller's copy of call-by-value parameters are unchanged by a called function, but call-by-reference values are (answer (a)). 9. Answer is (d), both (a) and (b). See page 197 of Deitel and Deitel 10. The 3rd element of the array is b[a-1] and the fifth is b[a+1], so b[a+1] = b[a-1] + 3 sticks the sum of 3 and the 3rd element into the 5th element. 11. Constant variables are read-only variables - they must be initialized with a constant expression and can't be subsequently modifed. They make programs more easily scalable - a number must be changed in only one spot. (answer (c)) 12. Referencing elements outside of array bounds does not result in a syntax error - rather it is a run-time error with possible devastating consequences, including changing data in an unrelated variable. (answer (a)). 13. Entire arrays are passed call-by-reference, but individual elements are just like other variables (if you have declared int arr[20]; then arr[i] is just an int) and are by default passed-by-value (answer b) 14. The qualifier const before a function parameter indicates that that parameter is read-only - no modifications are allowed inside the called function (answer b). 15. Bubble sort requires a maximum of n-1 passes, each swap requiring 3 assignments (an extra to store one of the values in a temporary location). The algorithm is simple, but a poor performer. Short Answer: 1. Parameter list:C Return value type: A Function body: D Function name: B 2. Output is 1 2 3 4 5 There is a space after each number, including the last one Since the increment is done in a statement that checks the result of the increment, the fact that it is a pre-increment is relevant. The counter is incremented before it is tested, so it stops at 5. 3. Since x and z are both ints, the / is integer division. Even though y is a float, it ends up with a value of 1.0 after the division. Next, 5.5x1.0 = 5.5, but this is truncated because x is an int. The answer is x = 5 4. The test (1 arr[i]) { smallest = arr[i]; } } return smallest; }