CS 1704 Fall 2003 Test 1 Answers Q A Reason -------------------------------------------------------------------------------- 1 5 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 9 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. Errors caught after deployment cost most to fix, because existence of the errors carry a cost for the users, and the fix must itself be deployed to users. 3 1 This is straight from the course notes. 4 8 This is also straight from the notes. Stroustrup's primary motivation was to make it possible for developers to create their own, custom data types that could behave as naturally as the built-in data types. All of the things listed here are noble goals, and some are related to the use of classes, but none are Stroustrup's motivation. 5 2 Straight from the notes. 6 4 F.Evaluate(0) will return the value of the polynomial at 0; just do the math. 7 8 F.Evaluate(1) == m + b, so F.Evaluate(1) - F.Evaluate(0) == m. 8 3 Straight from the course notes and class discussions. 9 3 The use of const on the parameter implies the operator cannot change its parameter, which is its right operand. The use of const after the parameter list implies the operator cannot change the object within which it runs, which is its left operand. 10 2 The operator adds two LinearPoly objects, and creates a new one which is returned. 11 3 A constructor must be invoked to create the proper object to return. 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. For questions 15 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. 15 3 The leak can be fixed by deallocating the target of Buffer... 16 3 ... immediately before a new target is allocated. 17 5 Line 4 allocates 100 bytes of memory. Line 9 is executed ten times, and so allocates another 1000 bytes of memory. Line 10 is executed once, and deallocates 100 bytes of memory. 18 3 The problem is the allocation is within the loop and there's no logical reason for it. The allocation before the loop is sufficient by itself, and would be properly undone by the deallocation given after the loop. 19 2 No, in that case the memory allocated in line 4 would never be deallocated. Also, there would be an access violation on the second loop pass, but that's incidental to the question. 20 2 The declaration should go in the header file designed to accompany the file containing the implementation of F(). That allows the function to be "exported" to any other source file that needs it. 21 3 F() calls G(), so the declaration of G() must be in scope in F.cpp; it would work to put the include in F.h, but then any other file that called F() would include F.h and so also import the declaration of G(), whether the declaration was needed there or not. 22 4 The type name Point is used in all three cpp files, so it should be declared in its own header file which can be imported wherever it is needed. 23 3 The directives can't prevent someone from #including the same header file as many times as they want. They just prevent the declarations inside the directives from being included more than once. 24 5 This should be self-explanatory. 25 4 2 is relevant because a function must be declared in order to be called, and the declarations of the member functions are in the header file. 3 is relevant because the implementation of each function uses the name Timer (in "Timer::" if nowhere else).