CS 1044 Test 2 Form A Key Fall 1998 Q A Explanation 1 5 I is decremented by 1 for each loop iteration, since 8 > 8 is false the loop halts when I == 8 2 6 The loop executes as I takes on the values I: 11, 10, 9, 8 the 4 iterations of the loop adds these values to J = 11+10+9+8= 38 3 7 Only the while & for are looping "iteration" control structures in C/C++ 4 5 j is decreased by 4 for each loop iteration, 4*19=76, so after 19 iterations j = 107 - 76 = 31 which is > 30, on the 20th iteration j = 27 which halts the loop 5 1 (see #4 above) 6 7 The first value in the input stream, 5 is read into mystery which is used to control the loop. The for loop reads the next 5 values from the input stream and and totals them into sum = 3 -2 + 4 + 1 + 7 = 13 7 8 The while loop reads all of the values from the input stream and adds them to sum = 3 -2 + 4 + 1 + 7 + 5 = 18 8 4 Sentinel while loop reads values from the input stream until a non-positive value is input & adds them into sum = 5 + 3= 8 9 5 The loop sequence is J = 2, 5, 8, 11, 14, 17, 20, 23, 26, 29 ... since J never == 28 the loop is infinite 10 6 (see previous explanation) 11 7 The outer i for loop executes 3 times 1..3, the inner j for loop also executes 3 times 1..3, since sum is NOT reset to zero each time the j loop restarts it is incremented by 3 each time the j loop executes 12 4 The outer i for loop executes 3 times 1..3, the inner j for loop also executes 3 times 1..3, since sum is reset to zero each time the j loop restarts it is incremented 3 times before each output = 3 13 9 Function Fix accepts a call by reference (address) int parameter followed by a call by value float parameter. Actual value parameters can be either a variable, constant or expression, but actual reference parameters must be variables. Only responses 2 & 4 meet this criteria. 14 7 Reference parameters allows called functions to communicate to the calling function in one direction by sending results out of the funtion by assigning new values to the reference parameters and to communicate two-way by resetting values of the reference parameters using the initial values passed into the function. 15 3 One-way communication (simplex) into a function is achieved by value parameters. Two-way communication in and out of a function is possible with eference parameters. 16 1 Value parameters cannot be altered by the called functions. 17 4 Ben is passed by reference to DoThis into Alpa. Thus Alpha = Alpha + 100 is equivalent to Ben += 100. 18 3 Jerry is passed by value, thus it cannot be changed by DoThis. 19 8 Temp in main is output and has local scpe to main(). It has no relation to the Temp in DoThis. 20 2 Any variable only accessible in a function has local scope to the function. Only local function variables or formal parameters have local scope. 21 5 The character letter grade variable should not be altered by IntEquiv, which must return an integer type. 22 3 LoopCount takes on the values 8 & 9 before the loop halts. 23 1 Since 71 must be the last value output only N <= 71 will produce the correct output. 24 2 All C/C++ arrays begin at index 0 and continue to the dimension-1. 25 2 Only loop 2 stores zero values in the valid array indexes Status[0] thru Status[9]. 26 3 The for loop outputs the array values in the reverse order from [4] .. [0] 27 8 Loop 1 index takes on the even values 0,2,4...4998 outputting the correct array positions and loop 3 index takes on the consecutive values 0,1,2,3...2499, when multipled by 2 it causes the correct array positions 0,2,4...4998 to be output. 28 7 Loop 3 would output "BenBenBen" and loop 4 would output null character in index 3 of myName. 29 2 C/C++ does not check array bounds so the code would execute, but it would access array locations that do not logicaly exist. 30 1 The call attempts to pass a single array variable to a function that requires the entire array to be passed. 31 2 The array dimension being passed it less than the actual dimension. The function would execute but would not logically fill the entire array. 32 3 Integer array locations can contain the indexes of other arrays, (indirect subscripting). 33 2 MaxValue does not need to alter values in the arary, but since arrays are passed by reference by default it must be passed by constant reference for safety. 34 1 MaxValue does not need to change the dimension of the array so it should be passed by value. 35 1 Since the loop begins at one comparing arrays values the zeroth location must assumed as holding the initial largest value. 36 2 The loop must examine each value from 1 to the array dimension - 1. 37 3 When a larger value is found, maxSoFar must be set to its index. 38 4 Enum variables cannot be directly output and their values are not quoted. 39 6 Enum type values by default are represented internally by integer values starting at zero. 40 1 'W' follows 'V' which places "WVU" alphabetically after 'VaTech", so strcmp returns a positive integer.