Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include <stream.h> int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3; x = f(y, z); cout << x << " " << y << " " << z << endl; A. 10 2 2 B. 10 2 6 C. 10 4 6 D. The program won t compile because the function call should have been x = f(y, &z); E. None of above 2. 3. 4. Which of the following statements about function declarations and definitions are true? A. The definition gives the implementation of the function; the declaration only contains the function name, result type, and parameters. B. A declaration may only appear in a.h file; a definition only in a.cpp file. C. Declarations and definitions may both appear in.h files. D. Statements A and B are true. E. Statements A and C are true. If Blob is a class, its implementation file blob.cpp should contain an #include "blob.h" directive to include the blob interface because A. it is only good style; there is no technical reason for doing so. B. It allows the compiler to check whether the implementation in the.cpp file is consistent with the interface in the.h file. C. It may not be possible to compile the implementations in the.cpp file without doing so. D. Both B and C E. Actually, the.cpp should not #include the.h file. This could generate duplicate declarations or definitions of items in the class, and the file would not compile. Suppose we find the following function in a file: int Abc::xyz(int z) return 2 * z + 1; What do we know by looking at this function? A. This function appears in file abc.cpp B. xyz is a member function of class Abc. C. xyz is a friend function of class Abc. D. We could call this function in an assignment statement k = Abc.xyz(6). E. Tow or more of the above statements are true.
Page 2 5. A class definition allows some items to be specified as public and others to be specified as private. If a variable x is specified in the private section of the class definition, there are several places where one might want to change its value by directly assigning a new value to x. Of these possible places: 1: public member functions of the class 2: private member functions of the class 3: functions declared as friends of the class 4: other functions appearing in the implementation file 5: functions that appear in other.cpp files Which of these can directly change the value of x? A. 1 and 2 only B. 2 and 3 only C. 1, 2, and 3 only D. 1, 2, 3, and 4 only E. 1, 2, 3, 4, and 5 6. Given the followin line: friend Thing & operator<< (ostream &s, const Thing &t); Which of the following is true? (1) This line of code must appear in the Thing class declaration in order for it to be a friend of Thing. (2) The ostream should instead be passed by value. (3) This definition will not allow output stream chaining (ie. cout << x << y). A. 1 only B. 2 only C. 3 only D. 1 and 2, but not 3 E. 1 and 3, but not 2 7. A complete C++ class definition should contain most of the following if the class refers to dynamically allocated data. Which of these are not needed? A. Null constructor B. Copy constructor C. Initialization function D. Destructor E. Operator=
Page 3 8. Suppose Blob is a class containing all of the standard features needed in a C++ class. Suppose we have the following declarations in a client program that uses the Blob class: Blob b1; Blob b2 = b1; // **** What happens when the second variable declaration (marked ****) is processed? A. Blob::operator= (assignment) is executed to initialize b2 with value b1. B. The null constructor is executed to initialize b2, then Blob::operator= is executed to assign the value of b1 to b2. C. The copy constructor is executed to initialize b2 with the value b1. D. The null constructor is executed to initialize b2, then the copy constructor is executed to copy the value of b1 and store it in b2. Suppose we have a program that allocates and frees an array as follows: char* s; s = new char[100]; The character array is freed properly and returned to the heap. The program will not compile because you are using the wrong form of delete. The program may continue executing and might crash later. The program will not compile because you are deleting something in automatic memory. the program will delete the first item in the array, and the rest of the array will become a memory leak. Variable j will not be changed, but k is changed to 3. Variable j will change to value 3, k to 7. Variable j will change to value 63 (ASCII code for? ), k to 3. Neither j nor k will be changed. variables j and k will be changed to some undefined value. Modify readinnumbers to use assert macros. Modify readinnumbers to return a status flag. Modify readinnumbers to return a "safe" value if user input failed. B and C A and B and C
Page 4 12. 13. A function declaration contains A. The function name only. B. The function name and function body. C. The function name, types of parameters, and type of return value. D. The function name, types of parameters, type of the return value, and function body. E. A #include to access the associated header file What does the following program output? void snark(int* a, int& b, int c) *a = c; b = *a + 1; c = c + 2; void main() int x = 3; int *y = &x; snark(y, x, x); cout << x << " " << y << endl; A. 3 3 B. 4 4 C. 3 4 D. 4 3 E. None of above. Given the following code snippet: int* gobble(int size) int* a = new int[size]; a[0]=0; for (int k=1; k < size-1; k++) a[k] = a[k-1]+k; return a; 1 3 6 Unknown, because there s a dangling pointer. Unknown, because there s a memory leak.
Page 5 Short Problem (8 pts) The following problem assumes the following class and function: class Sample public: Sample(); Sample(const Sample& other); Sample& operator= (const Sample& other); ; ~Sample(); Sample mixsample ( Sample& blend1, Sample& blend2 ) Sample result = blend1; return result; Given the following code snippet, and assuming item1 and item2 are of type Sample: Sample blend3; blend3 = mixsample ( item1, item2); Write down all of the Sample methods (and the ojects they are invoked on) that are called during the execution of the code, in the order in which they are called. Short Problem (8 pts) For the following program snippet, draw a boxes and arrows diagram representing what is happening in memory, when explicitly stated in the code. Draw a vertical line representing the division between automatic and dynamic memory. All memory that is automatically allocated is placed on the right side of the vertical line. You must clearly show what the pointer is pointing to; if the pointer contains NULL, make Draw the diagram at this point
Page 6 Long Problem (21 pts) A knapsack holds a collection of items and their associated weights. A knapsack is limited by both the number of different items it can store as well as the overall weight of the items that it currently carries. Implement the methods of the Knapsack class: const int maxnitems = 80; const int maxknapsackweight = 400; const int maxitemnamesize = 80; // Max. # of different items in sack // Maximum weight that a sack can hold // Longest name len