CS 1704 Fall 2003 Test 2 Answers Q A Reason -------------------------------------------------------------------------------- 1 1 #include is used to import header files, which contain declarations of classes and functions and other things which are implemented in one part of a program but used elsewhere. 2 1 The member functions of a class must be declared to be within the scope of that class (i.e. Node::function() ). 3 6 Straight from the notes and class discussion. 4 8 When used correctly, no errors would result; that eliminates 1 and 2. Conditional compilation will prevent code from being included, not cause more code to be included; that eliminates 3. 5 3 Line 1 just declares the pointer; it doesn't even have a value yet. Line 2 assigns the pointer a value, but no target has been created. Line 3 explicitly allocates an object and puts it address into the pointer, so NOW it has a target. 6 3 *p refers to the target of the pointer p. 7 8 Line 5 assigns the value of p to q; so q stores the address of the same target as p. 8 3 Trivial, given the answers to 6 and 7. 9 4 Since p and q have the same target, this statement changes the value of the target of p. 10 4 This deallocates the target of q, which was also the target of p. 11 4 The header comment implies that List is a pointer to an array of integers; this function must assign that pointer a new value since this function creates the array. So, the pointer must be passed by reference. 12 2 The function must allocate an array of ints, of the specified size. 13 2 Straight from the notes on deallocation. 14 1 After delete is called, List no longer has a target, but it also hasn't been reset to NULL. 15 7 1 won't work because it moves Head before resetting the Prev pointer in the second node (draw it out). 16 3 1 won't work because it sets Prev in the new node to point to the original first node. 2 won't work because it sets Head->Prev to NULL when the old first node should be made to point to the new node. 17 3 Head points to the first node; there's nowhere to save that value except in the pointer Save. 18 6 This requires resetting Head; since Head and Save both point to the first node after Line 3 is executed, either 2 or 4 will do. 19 8 You have to make the node that Save points to follow the node that Tail points to: Tail->Next = Save; 20 1 Given the answer to 19, this is obvious. 21 3 Straight from the class discussion on operator=. 22 4 Straight from the class discussion about the copy constructor. If the copy constructor took its parameter by value, that would involve a recursive call within its parameter list... not good.