Example Final Questions Instructions This exam paper contains a set of sample final exam questions. It is for practice purposes only. You ll most likely need longer than three hours to answer all the questions. 1
1. State whether each of the following statements is true or false, and briefly explain your reason. (a) If a function in a class is declared to be a virtual function, then it must NOT have definition (implementation) in that class. (b) You can not create objects from an abstract class, therefore an abstract class is useless. (c) If class A is derived from class B (i.e., class A inherits class B), then class A can overwrite (override) a private function defined in class B and class B s constructor. (d) The only way to implement a binary tree ADT is to use a pointer based data structure, because you have to use pointers to point to child nodes. (e) When you need to raise an exception, you have to put the throwstatement directly in a try-block. (f) If the condition in an assert statement is evaluated to be false, the function that contains that assert statement will be terminated, but the program can still continue. (g) The following two functions are allowed to be in the same class Data because their return types are different and they have different parameter names. void Data::updateStatus(int initialstatus); bool Data::updateStatus(int newstatus); 2. What is the precise output from the following complete C++ program? // Program begins here #include <iostream> using namespace std; class Data { public: Data(); ~Data(); void set(double x1, double x2); void display(); 2
static int count; private: int id; double *x; int Data::count = 0; Data::Data() { id = ++count; cout << "Initialize object " << id << ".\n"; x = new double[2]; x[0] = x[1] = 0.0; } Data::~Data() { delete [] x; count--; cout << "destroy object " << id << ".\n"; } void Data::set(double x1, double x2) { x[0] = x1; x[1] = x2; } void Data::display() { cout << "Object " << id << " has:\n"; cout << x[0] << " " << x[1] << endl; } int main() { Data a[2]; a[0].set(1.0, 2.0); a[1].set(3.0, 4.0); Data & b = a[1]; Data * c = new Data(); Data * d = &a[0]; c->set(5.0, 6.0); cout << "Total objects: " << Data::count << endl; b.display(); c->display(); 3
d->display(); delete c; cout << "Total objects: " << Data::count << endl; return 0; } // Program ends here 3. First, explain what is the ADT Dictionary. Then, describe the basic operations of a Dictionary. For each operation, describe its interface and what it should do. (Note: You do not need to implement this ADT.) 4. Given the following interface of class Window that describes the standard window objects: class Window { public: Window(); ~Window(); void setposition(int X, int Y); void setsize(int width, int height); void getposition(int & X, int & Y); void getsize(int & width, int & height); void display(); private: int positionx; int positiony; int width; int height; Design the interface of a message-window class, called MsgWindow. A message window is a standard window that is used to display some message. The methods used to handle the position and size of a message window should be the same as those in the standard window. However, the method used to display a message window is different from the one used to display a standard window. A message window also should store the message to be displayed and provide methods to update the 4
message and retrieve the message. Your interface design should show the public methods, the private data fields and the relationship between the class Window and the class MsgWindow. (Note: You do not need to implement the methods.) 5. Write a C++ function that takes two strings as parameters and finds and returns a string that contains the longest common prefix of these two strings. For example, the two strings abcxyz and abxyz have the longest common prefix of ab ; and the two strings abc and xyz have the longest common prefix of (empty string). 6. Given the following interface of a Queue class: class Queue { public: Queue(); ~Queue(); void enqueue(int data); int dequeue(); bool isempty(); private: int values[100]; // a circular array int front; int back; A circular array is used to implement this Queue class. Implement the method enqueue and isempty. 7. Given the following interface of a LinkedList class: class LinkedList { public: // create an empty linked list LinkedList(); // 1 // destroy each node in the linked list ~LinkedList(); // 2 // insert the data to the front of the linked list void insertfront(int data); // 3 5
// remove and return the data that s in the front // of the linked list, throw an exception // if the list is empty int removefront() throw (string); // 4 // return true if the list is empty bool isempty(); // 5 // return the data resides in the position indicated // by the index; if the index is less than 0 or // greater than or equal to the number of elements // in the list, throw an exception int & operator [] (int index) throw (string); // 6 private: struct Node { int data; Node * next; Node * front; // pointing to the front of the list int size; // the number of elements in the list Implement all six functions of this class. (You can decide whether or not you want to create a dummy node for the linked list, as long as the implementation of all the functions is consistent.) 8. Given the following struct definition: struct Data { string name; int number; Develop a C++ function, called loaddata, that opens a file and reads data from the file into an array. This function takes the following three parameters: (a) a string, called filename, which stores the name of the file to be opened; (b) a pointer pointing to an array of Data, called arr, which will be set to point to an array that stores the data; 6
(c) an integer, called arrsize, which will be set to store the size of the array created. If the file can be opened successfully, then you can safely assume that the first line in the file is an integer number indicating how many data items are stored in the file, and each data item occupies two lines, the first line contains a character string as name (may have white spaces) and the second line contains an integer as number. The function should open the file, read in the number of elements first, set the arrsize to be that number, dynamically create a Data array big enough to store all the data items and make arr pointing to this array, and then read the data items from file to the array, close the file and return true. If the file can not be opened successfully, then the function should return false. The prototype of the function is shown below: bool loaddata(string filename, Data *& arr, int & arrsize); 9. Write a C++ function, findmax, that takes an array of strings, called values, and the array s size, called size, as its parameters and returns a double value. Each string stored in array values should be able to be converted to a positive double number. The function findmax converts each string stored in the array values to the double number it represents and then finds and returns the maximum of these numbers. There are the following exceptions that need to be handled by try-catch-throw method in this function: (a) If the size of the array is zero, the function should throw an exception of type EmptyArray, catch it and simply return 0.0. (b) If any of the strings can not be converted successfully to a positive double number, the function should throw an exception of type WrongFormat out. (Note: There is no need for the function to handle this exception itself.) You can safely assume that the two classes EmptyArray and WrongFormat are already defined and can be used by function findmax. The constructors of both these two classes require no parameters. The function interface should include a throw list. 7
To convert a string to a double number, you can use the library function double atof(char*);. 10. Given the following binary tree: 6 4 8 1 10 3 7 12 (a) Show the in-order, pre-order and post-order traversals of the above shown binary tree. (b) If the number in each node represents the data stored in that node, is this binary tree a binary search tree? Why or why not? (c) Breadth-first traversal means to visit the nodes in a tree by layers, i.e., visit all the nodes with depth 1 first, then visit all the nodes with depth 2, and so on. Show the breadth-first traversal of the above shown binary tree. 11. The following picture shows a thread tree (with dashed lines representing thread pointers). 8
The struct of a thread tree node is defined below: struct TreeNode { string data; TreeNode *left; bool leftthread; TreeNode *right; bool rightthread; Write a C++ function, called threadtraversal, that takes a pointer to a thread tree s root as its parameter, and performs in-order traversal of the tree. The function should not use any extra ADTs and should not be a recursive function. The prototype of the function is shown below: void threadtraversal(treenode * root); You can safely assume that the function, visit, that performs actions on the data stored in the tree nodes has been developed (you do not need to implement this function). The prototype of function visit is shown below: void visit(string & data); 12. A set is a collection of distinct objects. You are asked to develop a class Set. To do that, a dynamically allocated array is used to store the objects and template is used so that different Set objects can be created to store different types of objects. The Set class should have the following methods: a default constructor, the default size of the array is 128. a constructor that takes an integer as its parameter and use that integer as the size of the array. a destructor that releases the array space. insert an object into the set. This function should return true if the insertion is successful, and return false if the insertion fails (i.e., the object is already in the set). 9
remove an object from the set. This function should return true if the object was in the set and the remove is successful, and return false if the object was not in the set at all. Your tasks include: (a) Design the interface of the templated class Set, including its public methods mentioned above and its private data fields. (b) Implement the two constructors and the destructor of class Set. (c) Write a block of main function code that declares two Set objects. One Set object is used to store integers and the size of the set is left as the default one. The other Set object is used to store pointers pointing to integers and the size of the set should be 200. ====== END OF EXAM QUESTIONS ====== 10