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 by main(). |
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 named data:
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. |
13.
If a base class pointer, p, points to a derived class instance, and both base and derived classes have a method void doIt() which takes no parameters, then p->doIt() will invoke the derived class version ...:
| A. | ... only if both the base class method and the derived class method are declared virtual. |
| B. | ...if the derived class method is declared virtual, but the base is not declared virtual. |
| C. | ... if the base class method is declared virtual, even if the derived class method is not declared virtual. |
14.
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)
Check the (one) true statement.
| A. | This expression is equivalent to the simpler expression
P AND Q |
| B. | This expression cannot be simplified to any of the options, above. |
| C. | This expression is equivalent to the simpler expression
P OR Q |
| D. | This expression is equivalent to the simpler expression
P XOR Q |
15.
Lazy Deletion, as we have implemented it in a general tree, is good for ...
(Check all that apply.)
| A. | ... conserving memory. |
| B. | ... fast removal.
|
| C. | ... fast insertion. |
| D. | ... avoiding garbage collection for a small number of removal calls. |
16.
Bob defines a base class, Base, with instance method func(), but does not declare it to be virtual.
Alicia (who has no access to Bob's source code) defines three derived classes, Der1, Der2 and Der3, of the base class and overrides func().
Ying has no access to Bob or Alicia's source code and does not know anything about the derived classes, including their names or how many derived classes exist. Ying does know about the Base class and knows about its public instance function func(). She is also aware that there may be derived classes that override func().
Ying uses a Base pointer, p, to loop through a list of Base object pointers. Ying is aware that the list pointers may point to Base objects or some subclass objects of Base.
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 (no coercion) and will always get the Base behavior of this function. |
| C. | Ying can call p->func() on each object in the loop and get distinct behavior as defined by the object pointed to if she uses type coercion to assist her. |
| D. | Ying can call p->func() on each object in the loop and get no compiler error. |
| E. | Ying can call p->func() on each object in the loop and get distinct behavior as defined by the object pointed to. |
17.
If a Boolean function does not have a simple algebraic or logical description then it is most easily implemented by a method that has which of the following?
| A. | Several user input statements with appropriate prompts to the user. |
| B. | A statement that uses multiplication and division. |
| C. | A long if statement. |
| D. | A for loop. |
| E. | An array. |
18.
Assume p is a private pointer member of class DeepClass that gets assigned dynamically allocated data in one or more of DeepClass's instance methods. p controls this dynamically allocated memory.
Check all that apply to the client statement myDeepB = myDeepA; between two DeepClass objects.
| A. | The default assignment operator that C++ provides will result in a compiler error. |
| B. | The default assignment operator that C++ provides will cause both myDeepA's and myDeepB's "p-controlled" memory be to simultaneously modified if myDeepA's is used to modify its copy of that memory (say, immediately after the assignment statement). |
| C. | The default assignment operator that C++ provides will immediately crash the program when the assignment statement is encountered during run-time. |
| D. | If DeepClass has a user-defined destructor that deallocates p's memory, but no overloaded assignment operator is provided, then the assignment statement, followed immediately by both objects going out-of-scope (as in the program or method in which both objects are defined, ending), will cause a run-time err |
| E. | The default assignment operator that C++ provides will result the dynamically allocated memory to be duplicated and copied over to myDeepB. |
19.
The following are advantages of a linked-list over an an array:
(Check all that apply.)
| A. | Even if we don't have a pointer to the proper node, a linked-list can easily access any element in the middle of the list with a single statement, but an array requires a loop to gain access to that node. |
| B. | A linked-list takes less memory to store a single element (a Node in the list) than an array takes to store its version of a single element (the kth item in the array). |
| C. | Inserting an item into an already-ordered linked-list, so as to preserve the order, requires fewer loops and/or a shorter loop than inserting an item into an ordered array so as to preserve the order. |
| D. | Assuming we have a pointer to the proper node, a linked-list will enable fast insertion into the middle of the list, while insertion into the middle of an array, even if we know the int index of the desired insertion point, will not be as fast or simple. |
20.
A Boolean function that has four inputs and one output. The programmer decides to implement it using an array of type bool. How many elements (what size array) gives the programmer the maximum flexibility in defining the function?
| A. | 3 |
| B. | 4 |
| C. | 16 |
| D. | 15 |
| E. | 8 |
21.
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