CS 1044 Test 2 Form A Key Spring 2000 Q A Explanation 1. 3 The value of I J: Prior to the loop: 6 0 In the first iteration: 5 -5 In the second iteration: 4 -9 In the third iteration: 3 -12 The main thing to remember here is that I counts down and J sums the neagative values of I after it is decremented. 2. 4 (See 1. Explanation above.) 3. 3 The value of J varq newq iteration Prior to the loop: 7 1.0 0.0 0 In the first iteration: 13 1.0 8.0 1 In the second iteration: 19 1.0 14.0 2 In the third iteration: 25 1.0 20.0 3 ............................ 73 1.0 68.0 11 79 1.0 73.0 12 Note that varq never changes, the loop controlled by J counts up by 6 starting at 7, and newq equals 1 more than J before it changes in each iteration. 4. 5 (See 3. Explanation above.) 5. 2 The loop executes only 1 time since 0 <= 0 (count <= total). Thus only 2 values from the file are read: 4 into mystery and 1 into value. 6. 7 Before the for loop the value of mystery is 4. Therefore, the loop is executed 4 times. The loop sums up 1, 7, 5 & 3. Therefore, the value of sum is 13. 7. 7 Initially the value of mystery read in is 4. It is then decremented by 1 in the loop. The loop continues until mystery reaches 0 or a negative number is read into value. The next 4 numbers after the first number (1, 7, 5, 3) are input until mystery is 0. Thus sum = 13 = 1 + 7 + 5 (the 3 is read but not added to sum.) 8. 2 The initial value of j is: j=28 After the first iteration: j=25 After the second iteration: j=22 After the third iteration: j=19 After the fourth iteration: j=16 After the fifth iteration: j=13, therefore the loop executes 5 times before J == 13. 9. 5 (See 8. Explanation above.) Note that when J becomes 13, the loop stops. 10. 6 The outer loop, i, executes for the values 0, 1, 2 the inner loop, j, executes for the values 0 & 1 (twice for each outer loop value). Sum is reset to 0 before each execution of the inner loop. Thus sum = 1 + 2 * i for each iteration of i. The 1 is the only value of j that gets added & sum doesn't add in between loops since it resets to 0. 11. 8 Similar to the above question, except that sum is NOT reset to 0 before the inner loop. Thus sum = sum + 1 + 2 * i for each iteration of i, which means sum = 9. 12. 9 In the function, the first parameter is pass-by-reference. Therefore, a variable must be specified for it. A value cannot be passed. The second parameter is pass-by-value, therefore, a numeric expression can be specified for it. 13. 2 Since pass-by-reference parameter communication is in both directions (2-way) and pass-by-value parameter communication is only into the called function (1-way), the first parameter must be passed by reference and the second by value. 14. 1 Since the variable 'Ben' is passed by value the changes made in the function are not reflected in the main program. Therefore, the value of 'Ben' remains -5. 15. 6 Since the variable 'Jerry' is passed by reference to the function DoThis. In the function DoThis 'Beta' is set to 999. Therefore, the value of Jerry becomes 999. 16. 2 Since Tmp is NOT a global variable and it is also defined locally in DoThis the changes to Tmp in DoThis has no effect on the Tmp variale in main. 17. 3 The key point to remember here is that rows are first and columns are second in a two dimentional array. Also the arrays indexes start from 0. 18. 1 The array elements are NOT changed by SmallValue so it should be passed by const int 19. 2 The number of array elements are NOT changed by SmallValue so it should be passed by value. 20. 2 Since the loop starts at 1 the initial value for smallest must be List[0] for all array values to be checked. 21. 5 When a smaller value in the array is located then smallest must be reset to that value, which is List[Lo]. 22. 7 The array locations 0 .. 3 are output by the loop. 23. 3 aRay[4] = bRay[4]; == aRay[4] = 10; 24. 2 cRay has a dimension of 4, but the Size passed to InitArray is 5 which will cause the InitArray function to access beyond the bounds of the array. 25. 1 The first parameter to InitArray must be an array and the second must be a simple int. 26. 3 cRay[aRay[1]] = bRay[Size-aRay[0]]; == cRay[2] = bRay[4]; == cRay[2] = 10; 27. 4 Line2 is concatenated on to the end of Line1 and stored in Line1. 28. 2 Line1 contains 7 characters, counting the spaces. 29. 1 Line2 is copied into (and replaces) Line1. strcpy(Line1, Line2); == Line1 = Line2; 30. 1 The 'i' in "Daniel" comes before the second 'n' in "Danny".