Study Guides (238,380)
Canada (115,122)
CS 246 (16)


11 Pages
Unlock Document

University of Waterloo
Computer Science
CS 246
John Holmes

UNIVERSITWATERLOO CS246INTE2008 CS246 Winter 2008— Midterm Thursday, 28 February, 2008 Instructor: M. W. Godfrey Time allowed: 120 minutes No aids allowed (i.e., closed book). There are 5 questions for a total of 80 marks. Plan your time wisely. In the immortal words of the late Douglas Adams, don’t panic! Answer all of the questions on this exam sheet. Use the blank exam booklets ONLY as scratch paper. Name: S OLUTION (Underline your last name) Student Number: (For marking purposes only) Q1 /20 Q4 /12 Q2 /20 Q5 /12 Q3 /16 TOTAL /80 Page 1 of 11 MIDTERM UNIVERSITY OW ATERLOO CS246, WINTER2008 1. [20 total marks] For each statement, circle T (true) or F (false), or do nothing. You get 2 points for a correct answer, 0 points for no answer, and −1 for a wrong answer. However, you cannot get less than 0 on this page. You may provide a short explanatory statement to support your answer if you desire, but it is not required. (a) T F Since a private method cannot be used by subclasses, it’s useless to declare it virtual. (b) T F The dynamic type of a pointer should always be the same as or an inheritance descendant of the static type. (c) T F Since abstract base classes cannot be instantiated, it is useless to define a con- structor for one. (d) T F A C++ object that is created via new will always reside on the heap. (e) T F A C++ object created on the stack will never have its destructor executed. (f) T F Within a given UML object model, a set of aggregation links cannot have cycles. T F In a UML class model, a derived class always inherits all of the associations of (g) its parent. (h) T F Copying a composite object usually involves “deep” copying. (i) T F In C++, only methods that are declared as virtual in the base class can be overridden in derived classes. (j) T F Java always uses dynamic dispatch. Page 2 of 11 M IDTERM U NIVERSITY OFWATERLOO CS246, W INTER2008 2. [20 total marks] Relatively short answer (a) [2 marks] Suppose that SilkPurse and SowsEar are C++ classes, and that SowsEar defines a public constructor of no arguments. Under what circumstances would the following statement be legal? SilkPurse *s = new SowsEar(); A NS: SilkPurse must be an inheritance ancestor of SowsEar. (b) [10 marks] A (multi-day) conference consists of a (non-empty) set of related one-day work- shops held at a hotel. Each hotel has a set of conference rooms and set of guest rooms; each room has a room number, and a rental rate. There are two kinds of attendees at these confer- ences: profs and students. A prof always gets his/her own guest room, while students have to share with each other (between two to four students per room). Each workshop must have at least 10 attendees. An attendee must attend at least one workshop. All attendees stay in guest rooms at the hotel. Draw a UML class diagram that models this situation. Be sure to include (as appropriate) associations, compositions, aggregations, inheritance, and multiplicities. Underline the names of classes that are abstract. Do not model attributes apart from the ones explicitly mentioned above. Do not model operations, or navigabilities. A NS: Page 3 of 11 M IDTERM UNIVERSITY OFW ATERLOO CS246, WINTER 2008 (c) [4 marks] What is a memory leak? ANS : A memory leak is a ”dead” object that was allocated on the heap but whose storage was never returned to the heap when the object’s use ended. There are no longer any pointers or references to the object; it is completely inaccessible to the running program. (d) [4 marks] Consider the following (compilable) C++ program: #include #include class Parent { public : void func() {std::cout << "Parent::func" << std::endl;} virtual void vfunc() {std::cout << "Parent::vfunc" << std::endl;} }; class Child : public Parent { public : void func() {std::cout << "Child::func" << std::endl;} virtual void vfunc() {std::cout << "Child::vfunc" << std::endl;} }; int main (int argc, char * argv[]) { ANS : Child c; Parent p; p.func(); c.func(); p.vfunc(); c.vfunc(); } Fill in the main program above so that the following output is produced (using appropriate instantiations and calls to the above methods): Parent::func Child::func Parent::vfunc Child::vfunc Page 4 of 11 M IDTERM UNIVERSITY OW ATERLOO CS246, INTER2008 3. [16 total marks] Consider the following (fully defined, compilable) class definition: #include #include #include class Chain { public: Chain (); void link (int n); void join (const Chain & c); void show () const; private: std::vector *ints; }; Chain::Chain (): ints (NULL) {} void Chain::link (int n) { if (ints == NULL) { ints = new std::vector ; } ints-> push_back (n); } void Chain::join (const Chain& c) { if (ints != NULL) { int size = c. ints-> size (); for (int i = 0; i < size; i++) { ints-> push_back (c. ints-> at (i)); } } } void Chain::show () const { std::cout << "Chain"; if (ints != NULL && ints-> size () > 0) { std::cout << ":" << ints-> at (0); for (int i = 1; i < ints-> size (); i++) { std::cout << "," << ints-> at (i); } } std::cout << std::endl; } Page 5 of 11 MIDTERM UNIVERSITY OFW ATERLOO CS246, W INTER2008 (a) [6 marks] Consider the following main program.
More Less

Related notes for CS 246

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.