CS 1704 Fall 2002 Test 1 Answers Q A Reason -------------------------------------------------------------------------------- 1 3 Up to a point, increasing the number of modules makes testing and managing design easier, decreasing total cost; after a point, as the number of modules increases the cost of managing the interfaces and inter-module communication drives the total cost up. 2 5 Errors caught during design are more easily corrected because there is no code through which changes must be propogated; errors caught during implementation and testing cost more to correct because the changes to code will generally involve a cascade of alterations to related modules. 3 6 1 is simply silly unless your customers are design freaks. 2 is valid, since the chart provides a way to record and communicate the communication interfaces of the modules. 3 is valid because the chart serves as a record of design decisions that would otherwise be buried in internal documentation. 4 is NOT a goal. 4 2 Straight from lecture. 5 2 Straight from the notes. 6 5 1 uses the automatic assignment operator that every class has. 2 uses a subtraction operator, which is in the class interface. 3 uses the type name as if it were a variable name. 7 1 const after a member function/operator means it is not allowed to modify the object it's running ON; that would be the LEFT operand. 8 2 The addition operator needs to create a NEW Money object and return it. 9 4 In general, objects should not be passed by value, in order to avoid the expense of copying; in this case, the operator should NOT be allowed to modify the actual parameter, so we pass it by constant reference. 10 4 Given the logic of the next few lines, this must add the two cents amounts together. 1 uses an unknown identifier "LHS". 2 doesn't add anything. 3 treats RHS like a pointer. 11 1 The calculation in line 3 may assign Sum.Cents a value greater than 99; line 4 calculates the carry that will go to the Dollar field; the cents amount would be the remainder. 2 takes the remainder from the wrong variable. 3 doesn't compute the remainder. 4 doesn't compute the remainder, and uses the wrong variable. 5 and 6 just don't make any sense. 12 4 Allocation of memory is accomplished in C++ by invoking new. 13 3 To assign a value to the target of a pointer, you must dereference the pointer, which requires using the dereference operator '*'. 14 1 The address to which the pointer points is simply the value of the pointer; to print the value of a variable you simply insert that variable into the output stream. 15 2 To obtain the address of a variable, you must use the address operator '&'. 16 3 The specified string is the target of the pointer, so this is essentially the same as question 13. For questions 17 and following, each pass through the loop causes the allocation of a new double; which is never deallocated. That memory is never deallocated. On the next pass through the loop, the next allocation changes the value of the pointer, losing access to the previously-allocated double. 17 2 The leak can be fixed by deallocating the target of p... 18 4 ... once we're done with the target, but before restarting the loop. 19 4 The loop body is executed 1000 times (Try goes from 0 to 999). Each pass results in leaking 8 bytes of memory (one double), EXCEPT FOR THE LAST PASS. (Sneaky.) But, the memory allocated on the last pass is also leaked with the pointer is reset to NULL immediately after the last pass. 20 2 The problem is that the allocation is within the loop and there's no logical reason for it. (In fact the same result could be accomplished without any dynamic allocation.) Moving the allocation statement outside the loop would eliminate the leak without breaking the logic. 21 1 The pointer p is passed by value to getAnInt(), so the function is working with a local variable pInt, not with the actual parameter p. Even so, the call to delete in the function deallocates the memory at the address that is stored in p; so the target of p IS deallocated. The function next allocates a new target for pInt, but that has no effect on p (pass by value). The target of p wasn't leaked because it was deallocated. 22 8 If the pointer p were passed by reference, then the function would be acting on it, deallocating its original target and then allocating a new target for p and assigning that target a value. Omitting the call to delete wouldn't work because the function would just be working with a local pointer. Same for omitting the call to new, and for reversing the calls. Passing const int* pInt would prevent the function from changing the target of the pointer; that wouldn't solve the original problem and in addition would cause a new one. Passing int* const pInt would prevent the function from modifying the pointer pInt, making the calls to delete and new illegal, but not addressing the real problem. 23 2 Transaction is an array of pointers to Money objects. The pointer at index 0 is Transaction[0]. To call a function on the Money object that is its target, you must dereference the pointer and call the function. Note: (*Transaction[0]).Value() would also work.) 24 5 The array isn't allocated dynamically. But each cell of the array is a pointer to a dynamically allocated object, and so delete must be invoked on each array cell. 25 3 Current points to the array cell Transaction[0]. After executing Current++, Current will point to the second array cell, Transaction[1]. Since the array cells will be allocated contiguously, the address of Transaction[1] will equal the address of Transaction[0] plus the size of one cell, which is the size of one pointer, which is 4 bytes.