Study Guides (390,000)
CA (150,000)
UW (7,000)
CS (400)
CS246 (20)
Final

CS246 Study Guide - Final Guide: Smart Pointer, Memory Leak, Object Copying


Department
Computer Science
Course Code
CS246
Professor
Ric Holt
Study Guide
Final

This preview shows pages 1-3. to view the full 12 pages of the document.
UNIVERSITY OF WATERLOO CS246, WINTER 2008
CS246 Winter 2008— Final Exam
Thursday, 4 April 2008
Instructor: M. W. Godfrey
Time allowed: 150 minutes
No aids allowed (i.e., closed book).
There are 6 questions for a total of K 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: SOLUTION
(Underline your last name)
Student Number:
(For marking purposes only)
Page 1 of 12 FINAL EXAM

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

UNIVERSITY OF WATERLOO CS246, WINTER 2008
1. [20 total marks] True / false
For each statement, circle T(true) or F(false), or do nothing. You get 2points for a correct answer, 0
points for no answer, and 1for 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) TFIf a method is not declared as virtual in a parent class, then it’s illegal to
override it in a child class.
(b) T F If a class embodies a aggregation relationship, then its destructor should almost
certainly call the destructor of all of its heap-based aggregate parts.
(c)
T F If your code might encounter an exception being thrown, then you should use a
“smart” pointer class such as auto pointer to refer to stack-based objects to
ensure that they don’t become memory leaks.
(d) T F The time complexity of performing an append onto the end of an instance of
std::list is best described as “amortized constant time”.
(e) T F An implementation-only class is one in which the constructors are public, but
all other methods (esp. inherited ones) are made private.
(f) TFCopying a composite involves making a “deep copy” of the object’s structure
and parts.
(g)
T F Suppose class Cinherits from class P, and that both provide a fully defined
method named m(). According to what we learned in class, the precondition of
C::m() should be stronger than (or the same as) that of P::m().
(h) T F If the destructor of a class is declared as private, it’s likely because the designer
intended for no client to create objects of that class.
(i) T F In C++, std::string::operator== uses object-identity semantics as its
model.
(j) T F If a method is declared as virtual, then static dispatch will be used to resolve the
call for an object on the stack.
Page 2 of 12 FINAL EXAM

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

UNIVERSITY OF WATERLOO CS246, WINTER 2008
2. [22 total marks] Short answer
(a) [2 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.
(b) [2 marks] Consider the code below. What is the general term for this? (I’m not referring to
exception handling.)
void f (Parent*p) {
Child*c = dynamic_cast<Child*>(p);
if (c==NULL) {
// do something drastic, like throw an exception
} else {
// happily treat the object as a Child
// ...
}
}
ANS:It’s called downcasting.
(c) [2 marks] This phenomenon (i.e., mentioned in part 2b) played a particularly prominent role
in the discussion of one design pattern. Name the design pattern in question.
ANS:It’s a big deal for the Composite pattern.
(d) [2 marks] If a class has a non-virtual destructor, what does this suggest about how you might
use the class in your code?
ANS:It strongly suggests that you should not create a class that inherits from it.
(e) [2 marks] Fill in the blank: A software unit exhibits to other
units when changes to those units are likely to affect it.
ANS:Tight coupling
Page 3 of 12 FINAL EXAM
You're Reading a Preview

Unlock to view full version