CS 1044 Spring 2002 Test 2 Form B Answers Q A Reason -------------------------------------------------------------------------------- 1 4 The dimension of an array must be a constant integral value. 2 7 Buffer[] has dimension 60, so the cells are indexed from 0 to 59. 3 8 The second parameter to Fix() is passed by reference, so the actual parameter must be a variable. That rules out 1, 3, and 4. 4 5 The loop body is entered with j having the values 4, 8, 16, and 32, so it's executed 4 times. 5 6 On the last pass, the loop body is entered with j == 32, the first statement sets j == 64, and that's what's printed. 6 2 string variables are passed by value by default, just like all other variables in C++, except arrays. 7 5 From the notes, assignment of one array to another is not supported. 8 3 For P1, the flow is OUT, so it must be passed by reference. For P2, the flow is IN, so it should be passed by value. 9 2 You have to keep track of what's passed by reference and what's passed by value. The reference to Sauron in the function Ring() binds to the global declaration since there's no local declaration of that name. 10 4 11 5 12 3 The sum has to be calculated anew for each line, so it needs to be reset to zero each time we start on a new line. 13 8 This is just the usual input-failure logic. Line 8 would be the priming read. 14 2 The check shouldn't be done until we're done with the current line, so it goes after the inner while loop. 15 2 The count control on the for loop guarantees it won't try to process any extra lines. 16 7 This just requires a tedious trace of the execution. 17 2 The array has dimension SZ, so the cells are indexed 0 to SZ-1. 1 would try to access one-past-the-end. 3 misses cell 0. 4 combines the errors in 1 and 3. 18 7 The code is flawed because it reads directly into the array. If there are more than 1000 entries, the 1000-th will be read and stored in cell 999 (correctly). The loop will make one more pass, since Read == 999, but immediately Read will be set to 1000 and then the input operation will try to store a value at index 1000, which does not exist. If there are fewer than 1000 entries, the last one will be read and stored at (say) index k. Now Read == k, which is less than SZ, and the input stream hasn't failed (yet), so one more pass is made. The logic is then the same as described above, and the first unused cell is targeted by the read operation, messing it up. 19 5 The number of values that were stored into the array would be the value of Read. The values would be indexed from 0 to Read - 1. 20 8 For the first line inside the function to compile, the identifier COMMA must have a declaration that's in scope. There's no declaration of COMMA inside the function, and it's not a formal parameter, so the only other possibility is that COMMA is declared globally. 21 7 readName() takes a struct variable as a parameter. 1 is syntactically illegal because the parameter List is an array, not a struct. 3 is logically incorrect because List[50] doesn't exist. 22 1 writeStaff() takes an array of Name variables as a parameter. 2 and 3 are syntactically illegal because the actual parameter is a single struct variable, not an array. 4 is just illegal syntax; List[] is only allowed as a formal parameter. 23 1 struct members are accessed by using the syntax .. 2 gets that backwards. 3 doesn't name the struct variable at all. 24 3 Similar to 11. 1 doesn't name a struct variable at all. 2 uses the type name Rectangle where you need a variable name. 25 6 1 works because it names the relevant coordinate variables directly: +------------------ Rectangle variable R | +--------------- Point variable that's a member of the Rectangle R | | +------------- member of the Point variable NW v v v R.NW.x 2 uses Point as if it were a variable name instead of a type name. 3 works because R.NW is a Point variable and you CAN assign two structs of the same type. 26 1 2 won't work because the insertion operator doesn't work with struct variables. 27 2 The loop has to start its work at the first cell, index 0. 28 1 The loop shouldn't look past the last used cell, index Usage - 1, but see the answer to the next question. Pos can't take a value larger than Usage - 1 into the loop. 29 3 From the description of the function, you need to compare each element (except the final one) to the following element, so you compare List[Pos] to List[Pos+1]. 30 3 From the description, this should copy the next element into the current location.