Question
1. Check all that apply. A. Since Ying's loop pointer, p , is type Base , no compiler error will occur. B. Ying can call
1. Check all that apply.
A. | Since Ying's loop pointer, p, is type Base, no compiler error will occur. | |
B. | Ying can call p->func() on each object in the loop and get distinct behavior as defined by the object pointed to. | |
C. | Ying can call p->func() on each object in the loop and force only base class behavior of that method by using type coercion. | |
D. | Ying can call p->func() on each object in the loop (no coercion) and will always get the Base behavior of this function. 2. |
Multiple Inheritance
Consider a derived class B that is multiply inherited from base classes A1 and A2. Which would be a reasonable example of classes A1, A2 and B. (There may be more than one correct answer.)
A. | A1 is a person who has some degree in math: DegreeHolderInMath A2 is a person who has some degree in computer science: DegreeHolderInCS B is DegreeHolderInBothMathAndComputerScience | |
B. | A1 is an Insect (animal without a backbone, body in three sections, skeleton on the outside, usually give birth as eggs) A2 is a Mammal (animal with backbone, has hair/fur, usually give birth alive) B is an Animal (any living being on Earth which is neither a plant (vegetable) nor fungus) | |
C. | A1 is a RealEstateProperty A2 is an ItemForSale B is a RealEstatePropertyForSale | |
D. | A1 is a Condominium (One unit in a condo development in which a family might own and reside) A2 is a SingleFamilyDwelling (One house+land in which a family might own and reside) B is a ResidentialProperty (House+land, condo unit, townhome, or any other place in which a family might own and reside) | |
E. | A1 is an Arm A2 is a Leg B is a HumanBody 3. |
Select the derived class constructor definitions that represent actual constructor chaining (never mind the reason or effect, as long as it demonstrates a derived class constructor chaining to a base class constructor). There is more than one correct answer.
A. | SubClass::SubClass(int a) : BaseClass() { // other stuff } | |
B. | SubClass::SubClass() : BaseClass(4) { // other stuff } | |
C. | SubClass::SubClass(int a) { BaseClass(a); // other stuff } | |
D. | SubClass::SubClass(int a) { BaseClass::BaseClass(a); // other stuff } | |
E. | SubClass::SubClass(int a) : BaseClass(a) { // other stuff 4. |
Consider the boolean expression (where XOR is the exclusive-or operator defined in the modules or throughout the internet):
(P OR Q) XOR (P AND Q)
Match the inputs P and Q with the output that this expression produces (assume 0 = false and 1 = true).
Choices - use a choice only once | |
A. | P = 1, Q = 0 |
B. | P = 1, Q = 1 |
Match each of the following to a choice | ||
1. | 1 | Select A. P = 1, Q = 0 B. P = 1, Q = 1 |
2. | 0 | Select A. P = 1, Q = 0 B. P = 1, Q = 1 |
5.
The is a sum-the-data-in-the-tree question. It asks whether a method, sumAll() is a well-written recursive method. You will see three different versions of this question throughout the exam, but the opening assumptions, are identical for all such versions. The only difference between the various questions is the code that implements the method sumAll().
Assumptions:
The general tree in this problem is assumed to be physical, i.e., there is no lazy or soft deletion designed into this tree.
We are considering a recursive work-horse method to sum all the (assumed) integer data of the sub-tree.
The sub-tree is specified by the root pointer passed in. As usual, some client would pass a root to this method, then this recursive method would generate other (child or sibling) roots to pass to itself when recursing.
The members sib and firstChild have the same meanings as in our modules.
True of False:
The method, as defined below, is a good recursive method for summing the data of the sub-tree, based at the root node passed in.
To be true, it must satisfy all the following criteria. If it misses one, it is false:
It gives the right sum for the sub-tree, that is, it does not miss counting any data.
it does no unnecessary work, micro-management or superfluous testing.
It covers all situations (empty trees, NULL roots, handles all the children, etc.).
int TreeClass::sumAll(Node *root)
{
int sibSum, thisSum, childrenSum;
FHsdTreeNode
if (root == NULL)
return 0;
// set to 0 for next computations
sibSum = childrenSum = 0;
if (root->sib == NULL)
sibSum = sumAll(root->sib);
for ( child = root->firstChild; child != NULL; child = child->sib )
childrenSum += sumAll(child);
thisSum = root->data;
return childrenSum + sibSum + thisSum;
}
There are three true-false questions that start out the same in this exam, but each has a different method definition. You may want to come back and review your answer to this question after seeing the other method definitions. Only one of the three is true, and the other two are false.
True
False
TRUE OR FALSE
6.
It the client requests the removal of a single node in a general tree (a tree with no limit on the number of children per node) as we have studied it, this might result in many more nodes than the one requested to be removed (from the client's perspective).
While the answer will the same for normal and lazy deletion, you can assume this tree does normal, not lazy, deletion, to avoid unnecessary thinking time.
True
False
7.
Method overriding happens when ...
A. | ... a derived class method of the same name as a method in the base class takes the same number and type of parameters as the base class method. | |
B. | ... a derived class method of the same name as a method in the base class takes a different number or type of parameters as the base class method. |
8.
A class MyClass is instantiated by some client main() dynamically, using some MyClass pointers local to main(). Check all that apply.
A. | If no deep memory is used in MyClass (i.e., it contains only simple data, no pointer members), then main() does not need to manually delete the objects that it dynamically allocates. | |
B. | It is possible that MyClass does not need any of the following: a user defined destructor, copy constructor or assignment operator. | |
C. | main()is responsible for deleting the two objects it instantiated, either directly or by calling some method that deletes them. | |
D. | The situation described means that MyClass will always need a user defined destructor, copy constructor and assignment operator. | |
E. | It is MyClass's instance methods that are responsible for deleting the two objects instantiated bymain(). |
9.
Assume MyClass defines two nested exception classes, BadNumber and BadString. Also, assume that MyClass's method doSomething() throws these exceptions under certain error conditions, and returns without throwing anything when it decides no error has occurred. We call doSomething() four times in a try/catch block:
MyClass a;
try
{
a.doSomething();
a.doSomething();
a.doSomething();
a.doSomething();
}
catch (MyClass::BadNumber)
{
cout << " bad number " << endl;
}
catch (MyClass::BadString)
{
cout << " bad string " << endl;
}
catch (...)
{
cout << " just generally bad " << endl;
}
Check the true statements (there will be more than one correct answer).
[Ignore whether spaces or ' ' are displayed - it is the words of the cout statements that we are interested in.]
A. | It is possible that doSomething() will return without error in one or more of these invocations, yet we still have one or more of "bad number", "bad string" or "just generally bad" displayed on the screen after this block. | |
B. | It is possible none of "bad number", "bad string" or "just generally bad" is displayed on the screen after this block. | |
C. | It is possible that this block will be called without anything being displayed on the screen. | |
D. | It is possible neither "bad number" nor "bad string" is displayed on the screen after this block. | |
E. | It is possible both "bad number" and "bad string" is displayed on the screen after this block. |
10.
C++ STL lists have which of the following properties?
(Check all that apply.)
A. | They do not have STL iterators associated with them. | |
B. | They enable us to insert new. randomly generated, items into the list in some pre-determined order, without having to supply our own client-designed logic or code. | |
C. | They are supplied to us as a template class, meaning we can declare, in one statement, a list of user-defined class data just as easily as if we were to define a list of some primitive type. | |
D. | They do not require that any ordering relation (like an operator<()) be defined for the underlying data type. |
11.
MyClass has an internal 2-D array of dynamically allocated doubles, pointed to by the member nameddata:
class MyClass
{
private:
double **data;
int width, height;
// other stuff
}
Assume width and height are set by constructors and mutators, and the class deals with them and all other data management correctly. Here is the method we are interested in analyzing:
void MyClass::allocateDynArray(int newHeight, int newWidth)
{
int row;
if ( !valid( newHeight, newWidth ) )
return;
height = newHeight;
width = newWidth;
// delete and NULLs the data
deallocateDynArray();
data = new double*[height];
for ( row = 0; row < height; row++ )
data[row] = new double[width];
setArrayToAllZeros();
}
Check the true statements (there will be one and possible more):
A. | setArrayToAllZeros() needs parameters to know what bounds to use for its (likely) internal loops. | |
B. | A destructor is essential for this class. | |
C. | We have to set data = NULL before we do our error return near the top | |
D. | The invocation of deallocateDynArray() needs to be repositioned to a different place in the code in order to avoid a potential crash or memory leak. | |
E. | It's fine as is. |
12.
A C++ stl vector (check all that apply) ...
A. | ... is a type of pre-defined (in stl) template class. | |
B. | ... is a type of container. | |
C. | ...can be accessed using notation practically identical to (or identical to) a simple array. | |
D. | ... is a type of constructor. |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started