Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Multiple choice questions Answer on Scantron Form 4 points each (100 points) 1. Which is NOT a reasonable conclusion to this sentence: Multiple constructors for a class... A. are distinguished by the number of parameters and by the return type. B. are natural to have if there is more than one way to initialize a class instance. C. are an example of "overloading". D. all must have the same name. E. give the clients some choices about how to initialize an object. 2. Which is a good reason for defining private members of a class? A. So that the client cannot change internal data structures. B. So that you can provide accessor functions like getData(). C. Because private is the default mode for class instance variables. D. Because it is more efficient to access private data than public data. E. All of the above. 3. An example of a FIFO data structure would be a: A. List. B. Stack. C. Queue. D. Set. E. Table. 4. Local variables... A. are automatically allocated and automatically deallocated. B. are sometimes automatically allocated and sometimes dynamically allocated. C. are initialized by the system to zero (if numeric or pointer type). D. may be explicitly deallocated by the programmer (if desired), although this is not necessary. E. must all be declared at the beginning of the function. 5. int * countingList (int n) { // line 0 int * nums = new int[n]; // line 1 for (n=n ; n>= 0; n--) // line 2 nums[n] = n; // line 3 return nums; // line 4 } The most serious problem with this code is: A. line 0 is in error, because int * cannot be used as a return type. B. line 1 is an error, because int[n] is illegal. C. In line 2, it is illegal to modify the parameter n, because it was not passed by reference. D. line 3 will have an array bounds error at one point in the loop E. line 4 is an error, because it creates a dangling pointer. 6. Suppose you see this in a correctly working program: Myclass * v1 = new Myclass [3]; From this alone, you can conclude with certainty: A. Myclass has a copy constructor. B. Myclass has a constructor with one argument. C. Myclass has a destructor. A. A only B. B only C. C only D. None of A, B, or C E. More than one of A, B, or C 7. Suppose you have a class mammal originally defined as: class mammal { public: mammal (int); ... private: int ribs; double brainSize; ... }; Suppose you now want build a new class "whale" and inherit from mammal. The new class will need a data member called "fins" and a new constructor that takes one argument. "whale" also needs access to ribs and brainSize. What changes must you make to mammal to support this? A. Change public to protected. B. Change private to protected. C. Add the "fins" variable to the mammal class. A. A only B. B only C. C only D. A and C E. B and C 8. A commonly accepted view of Object-Oriented Programming holds that is is characterized by three things: A. Encapsulation, inheritance, and dynamic dispatching. B. Classes, recursion, and top-down design C. Functional decomposition, abstraction, and overloading. D. Post-modernism, constructivism, and cognito-intellectual deconstructionism. E. Objects, messages, and methods. 9. The worst case complexities for Linear and Binary Search are: A. linear: O(1) binary: O(N*logN) B. linear: O(N) binary: O(N) C. linear: O(N) binary: O(logN) D. linear: O(N*N) binary: O(N*Log N) E. linear: O(N) binary: O(N*N) 10. Given the following code: void DoIt() { MyClass c; ... } The destructor for c is called automatically A. When c is destroyed at the end of the program. B. When c.~MyClass() is called in the program. C. When c is destroyed at the end of DoIt(). D. If c uses dynamic memory. E. If c does not use dynamic memory. 11. Which of the following statements about separate compilation is true: A. All .h files are compiled before the .cpp files. B. If an implementation file is changed, it is only necessary to recompile that implementation file. C. If a specification file is changed, it is only necessary to recompile the corresponding implementation file. D. If an implementation file is changed, then all the files that the implementation file depends on must also be recompiled. E. If an implementation file is changed, then all the files that rely on the corresponding specification file need to be recompiled. 12. A "deep" copy may be prefered over a "shallow copy" because: a. Deep copying is more efficient than shallow copying. b. Deep copying prevents inadvertent updates of shared structures. c. Deep copying allows the default destructor to be used in all cases. A. Only a is true. B. Only b is true. C. Only c is true. D. a and b are true. E. none are true. 13. Suppose an expression tree has inorder traversal 1 + 2 * 3 + 4. What is the value of the expression? A. 11 B. 13 C. 15 D. 21 E. Cannot be sure 14. Which of the following statements are true: 1) 2n + 1 = O(n*n) 2) n*n = O(n*n-100) A. 1 is false, 2 is false B. 1 is false, 2 is true C. 1 is true, 2 is false D. 1 is true, 2 is true E. cannot be sure without knowing the value of n 15. bool F(int low, int high) { if (low >= high) return false; else return ! F(low + 2, high + 1); } Consider the state of A and B after the following two calls: bool A = F(5, 7); bool B = F(0, 4); A. A is false, B is false B. A is false, B is true C. A is true, B is false D. A is true, B is true E. The program does not terminate 16. Suppose that the class Snark has a copy constructor. At what points when in the following function is the copy constructor used? Snark F(Snark s) { Snark t(s); t.snipe(); return t; } A. Only when t is created. B. Only when the argument is passed to F and when the value is returned C. Only when t is created and when the snipe method is called D. Only when the argument is passed to F, when t is created, and when the value Is returned. E. Snarks can’t have copy constructors. 17. class A { public: virtual void fun(); }; class B : public A { public: virtual void fun(); }; void A::fun() { cout << "I am A. "; } void B::fun() { cout << "I am B. "; } Suppose the following statements are executed - what is the result? A* a = new B; B* b = new A; a->fun(); b->fun(); A. I am A. I am B. B. I am A. I am A. C. I am B. I am A. D. I am B. I am B. E. Nothing, since the compiler would report an error. 18. Consider the following four statements: A. Although quicksort has average case run time of O(n log n), in the worst case quicksort can take quadratic time. B. If a function f(n) is always less than another function g(n), then f(n) = O(g(n)) C. Given an arbitrary binary tree, a postorder traversal of that tree will visit the nodes in the exact reverse order of a preorder traversal. D. Different binary search trees can have the same inorder traversal. A. A. is false B. B. is false C. C. is false D. D is false E. A, B, C, and D are true. 19. Consider the following four statements A. For a queue, "enqueue" and "dequeue" cancel each other out: an enqueue of any value followed by a dequeue leaves the queue in exactly the state it was in initially. B. assert( true ) at any point in a program will cause that program to immediately abort. C. The bool type with the values true and false, and the int type with the values 1 and 0 can both be used to represent boolean values, but 1 and 0 should be used when possible because it makes the program faster. D. Programs which have short variable names are generally faster than programs with long variables names. A. A is true B. B is true C. C is true D. D is true E. The statements A, B, C, and D are all false 20. Consider the following code fragment int k = 0; for (int i = 0; i < N; i += 1000) for (int j = 0; j < N; j += 1000) k++; double d = 0; double v = N * 0.001; while (d < 1000) d += v; Which statement most accurately characterizes the run time (as a function of N): A. O(N*N) but not O(N) B. O(N) but not O(N*N) C. O(1000000 N) D. O(N*N*N) but not O(N*N) E. The asymptotic run time depends on the compiler that is used. 21. int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int whatisthis(int b[], int size) { if (size == 1) return b[size - 1]; else return b[size] + whatisthis(b, size - 1); } What is the result of the function call whatisthis(a, 4)? A. 10 B. 16 C. 24 D. 26 E. 30 22. Consider the following four statements A. In a binary tree with two nodes, the root is not a leaf node. B. A binary tree with n nodes has height at least O(log n). C. Finding an element in a binary search tree with n nodes runs in worst case O(n) time. D. Deleting a node in a binary search tree takes worst case O(log n) time. A. A is false. B. B is false. C. C is false. D. D is false. E. A, B, C, and D are true. 23. Suppose you are using a bucket hashing scheme in which each slot in your hash table contains a linked list in order to handle collisions. If you add n items to the hash table and they all happen to hash to the same location, how much time would you expect it to take to retrieve an item from the hash table? A. O(1) B. O(log n) C. O(n) D. O(n log n) E. O(n*n) 24. Given the following statements: 1) Pure virtual functions are only present in abstract classes. 2) You can instantiate classes with pure virtual functions. 3) Pure virtual functions can be in the protected section of a class. Which of the following are true? A. statement 1 only. B. statement 2 only. C. statement 3 only. D. statements 1 and 3 only. E. all statements are true. 25. Given the following classes and function: class Base { public: void print( ); virtual int value( ); }; class Derived : public Base { public: void print( ); virtual int value( ); }; void Foo(Base* basePtr) { basePtr->print( ); } If the print( ) methods both call the value( ) method, and Foo is invoked with a Derived argument, which of the following is true about when basePtr->print( ) gets called? A. The Derived print( ) method is invoked, which calls the Base value( ) method. B. The Derived print( ) method is invoked, which calls the Derived value( ) method. C. The Base print( ) method is invoked, which calls the Base value( ) method. D. The Base print( ) method is invoked, which calls the Derived value( ) method. E. None of the above; you cannot pass a Derived argument to Foo( ). Answer the following problems directly on the test paper. 1. 20 points The following struct is used to define nodes of a doubly linked list: struct DoubleNode { DoubleNode* next; DoubleNode* prev; int data; }; Give the code which removes a node from a doubly linked list, and returns the node to the free storage pool. (Don’t forget that next and/or prev could be null, if the node is at the start or end of the list.) void SpliceOut(DoubleNode* dPtr) { assert(dPtr != NULL) dPtr->prev->next = dPtr->next; if (dPtr->next != NULL) dPtr->next->prev= dPtr->prev; delete dPtr; } } 2. 20 points The depth of a node in a tree is the distance from the node to the root of the tree. (The root has depth 0). Give a recursive routine which determines the depth of a node with a given key value in a binary search tree. If there is no node of the given key value, then -1 is returned. The run time of your algorithm should be proportional to the height of the tree. Assume that tree nodes have the following structure: struct BSNode { BSNode* left; BSNode* right; int key; }; int FindDepth(BSNode* root, int k) { It’s probably safe to skip this one. } 3. 10 points Construct the binary expression tree for ((x + y) * z) + 2. Then show the pre-order, in-order, and post-order traversals of the tree. Tree: + / \ * 2 / \ + z / \ x y Pre-order: +*+xyz2 Post-order: xy+z*2+ In-order: x+y*z+2 4. 10 points Show the result of hashing the following integers into a table of size 9 using the hash function h(x) = 3x % 9. Assume that the buckets of the hash table are linked lists. Values to hash: 5, 10, 15, 20, 25, 30, 35, 40, 45 Skip this one 6. 10 points Show the result of inserting 18, 14, 9, and 8 into the following binary search tree. (Show the tree after the four values have been inserted in the order given. Make sure that the it is clear which are left children, and which are right children). 10 5 3 15 7 12 18 will be inserted to the right of 15 14 will be inserted to the left of 18 9 will be inserted to the right of 7 8 will be inserted to the left of 9 Show the result of delete the node 10 from the following binary search tree 13 moves to the root position. 15 is now the right child of 13. 14 is the left child of 15. 10 7 15 9 8 13 14 7. 10 points Give short answers to the following questions. a.) If you were implementing an inheritance hierarchy of students, would you make 143_student a subclass of student, or student a subclass of 143_student? Why. 143_student is a subclass of student because it is a “kind of” student. b) What is the difference between dynamic dispatch and static dispatch? Dynamic dispatch happens at run-time. Static dispatch happens at compile time. c) Give an example of a “has-a” relationship. Is this something that is generally implemented with inheritance? To steal Ernesto’s example. A car has an engine. But an engine is not of the same kind as car, therefore inheritance does not apply. 8. 40 points A priority queue is a data structure that stores values with associated priorities. The insert operation stores a value with a priority in the structure. The findBest operation returns the value with the smallest priority, and the deleteBest operation returns the value with the smallest priority and also deletes that value from the priority queue. For example, if the queue contains the following items (in (data, priority) pairs): 124, 3 123, 6 456, 7 234, 1 The subsequent deleteBest operations will return 234, 124, 123, and finally 456. The specification of a priority Queue is as follows: class PriorityQueue { public: PriorityQueue(); bool isFull(); bool isEmpty(); // Test if the priority queue is full // Test if the priority queue is empty // Add a value, priority pair to the queue void insert(int data, int priority); int deleteBest(); // Return and delete the value with the // smallest priority int findBest(); // Find the value with the smallest // priority private: // Fill this in EntryList entries; }; Complete the specification of the PriorityQueue above and provide implementations for the methods. Be sure to use some ADT as the backbone of your PriorityQueue in order to expedite the implementation process. Note that the data and priority tags are integer values. You may assume the existence of an EntryList, which is a list of items of type Entry. An Entry is a struct with a priority field and a value field. struct Entry { int data; int key; }; // The value // The priority If necessary, you may add other methods (but you are not required to). PriorityQueue::PriorityQueue() { EntryList has it’s own constuctor. } bool PriorityQueue::isFull() { return entries.isFull(); } bool PriorityQueue::isEmpty() { return entries.isFull(); } void PriorityQueue::insert(int data, int priority) { } int PriorityQueue::deleteBest() { } int PriorityQueue::findBest() { }