Question
25. 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
25.
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;
if (root == NULL)
return 0;
sibSum = sumAll(root->sib);
childrenSum = sumAll(root->firstChild);
thisSum = root->data;
return childrenSum + sibSum + thisSum;
}
HINT: 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
26.
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.
A. | A non-destructor instance method of DeepClass may not deallocate the memory that p controls. | |
B. | The memory that p controls will be deallocated when a DeepClass object goes out of scope if an appropriately written user-defined destructor is provided for DeepClass. | |
C. | The memory that p controls will be deallocated when a DeepClass object goes out of scope automatically by C++'s default destructor. | |
D. | Memory that p controls must be allocated at object construction. | |
E. | A user-defined destructor of DeepClass can deallocate the memory that p controls. |
27,
Which describe a design in which B is a derived class of base class A? (Check all that apply.)
A. | Class A is Building that shelters people and equipment. Class B is a HighRise, which has a minimum of five floors (stories) with stairs and elevators and utilized by government agencies, condo complexes and companies. | |
B. | Class A is an AutoPart which goes into a car or truck. It has a part number, weight and price. Class B is a Automobile -- either a Car or a Truck. | |
C. | Class A is Building that shelters people and equipment. Class B is a CityVisualizer which will render one or more Buildings in 3D on a computer or smart phone screen. | |
D. | Class A is an AutoPart which goes into a car or truck. It has a part number, weight and price. Class B is a FuelInjector, a special kind of part that goes into cars and trucks. |
28.
Assume that a class, Deep, has a pointer member
int *somePointer;
which will be assigned some dynamically allocated (deep/heap) memory during object construction (in the constructor).
Assume, further, that we have not overloaded the assignment operator for class Deep.
Two Deep objects, deepA and deepB are involved in an assignment statement:
deepB = deepA;
Check the true statements:
(There may be more than one.)
A. | deepB's somePointer will have its own copy of int memory associated with itssomePointermember after the assignment statement (distinct from deepA's) | |
B. | deepB's somePointer will be undefined (unaffected) by this assignment statement. | |
C. | This is a shallow copy; deep memory controlled by deepA is not copied and pointed to by deepB. | |
D. | After this assignment, both deepA and deepB will have their respective somePointers pointing to the same exact memory. |
29.
Check the true statement out of the two statements below (there is only one correct choice):
A. | Without type coercion being employed, a derived class pointer can only point to a derived class object, not a base class object. | |
B. | Without type coercion being employed, a base class pointer can only point to a base class object, not a derived class object. |
30.
Which describe a design in which B is a derived class of base class A? (Check all that apply -- there may be one or more checked box.)
A. | Class A is an MensApparelItem, which is stocked and sold by a department store like Macy's. Class B is an InventoryItem, something a business stocks and sells to a customer. It has an item number and price. | |
B. | Class A is an Sleeve, something that goes into a shirt, jacket or sweater. Class B is a Jacket. | |
C. | Class A is an InventoryItem, something a business stocks and sells to a customer. It has an item number and price. Class B is an MensApparelItem, which is stocked and sold by a department store like Macy's. | |
D. | Class A is a Jacket something that is worn to keep a person warm. Class B is an JacketSleeve (something that goes into a Jacket). |
31.
Match each data structure with its best description.
Choices - use a choice only once | |
A. | Data is both LIFO and FIFO: The are potentially two data items that could be naturally fetched (returned) to the client at any given moment, either the the item most recently stored or the oldest (earliest) item stored. |
B. | Data is neither LIFO nor FIFO: Client can naturally fetch (return) any of the many data items stored. |
C. | Data is a FIFO: There is only one data item that is naturally fetched (returned) to the client at any given moment, and it is the oldest (earliest) item stored. |
D. | Data is a LIFO: There is only one data item that is naturally fetched (returned) to the client at any given moment, and it is the item most recently stored. |
Match each of the following to a choice | ||
1. | Queue | Select A. Data is both LIFO and FIFO: The are potentially two data items that could be naturally fetc... B. Data is neither LIFO nor FIFO: Client can naturally fetch (return) any of the many data ite... C. Data is a FIFO: There is only one data item that is naturally fetched (returned) to the cli... D. Data is a LIFO: There is only one data item that is naturally fetched (returned) to the cli... |
2. | Stack | Select A. Data is both LIFO and FIFO: The are potentially two data items that could be naturally fetc... B. Data is neither LIFO nor FIFO: Client can naturally fetch (return) any of the many data ite... C. Data is a FIFO: There is only one data item that is naturally fetched (returned) to the cli... D. Data is a LIFO: There is only one data item that is naturally fetched (returned) to the cli... |
3. | Deque | Select A. Data is both LIFO and FIFO: The are potentially two data items that could be naturally fetc... B. Data is neither LIFO nor FIFO: Client can naturally fetch (return) any of the many data ite... C. Data is a FIFO: There is only one data item that is naturally fetched (returned) to the cli... D. Data is a LIFO: There is only one data item that is naturally fetched (returned) to the cli... |
4. | Array | Select A. Data is both LIFO and FIFO: The are potentially two data items that could be naturally fetc... B. Data is neither LIFO nor FIFO: Client can naturally fetch (return) any of the many data ite... C. Data is a FIFO: There is only one data item that is naturally fetched (returned) to the cli... D. Data is a LIFO: There is only one data item that is naturally fetched (returned) to the cli... |
32.
Match the diagram with the description.
Choices - use a choice only once | |
A. | This diagram describes multiple inheritance. |
B. | This diagram describes standard, simple inheritance |
Match each of the following to a choice | ||
1. | Base1 Base2 \ / \ / \ / \ / \ / Derived | Select A. This diagram describes multiple inheritance. B. This diagram describes standard, simple inheritance |
2. | Base / \ / \ / \ / \ Derived1 Derived2 | Select A. This diagram describes multiple inheritance. B. This diagram describes standard, simple inheritance |
33.
Assume base class has an overridden method called methodX(). Let baseObject and derivedObject be base class and derived class objects, respectively.
Check the true statements (there will be one or more):
A. | derivedObject.methodX() will directly invoke the base class version of methodX(). | |
B. | baseObject.methodX() will directly invoke the derived class version of methodX(). | |
C. | baseObject.methodX() will directly invoke the base class version of methodX(). | |
D. | derivedObject.methodX() will directly invoke the derived class version of methodX(). |
34.
Searching an array using a binary search (check all that apply):
A. | ... requires a pre-sorted array in order to work. | |
B. | ... is usually faster for each search than a simple linear search. | |
C. | ... requires more code and logic than a simple linear search. | |
D. | ... is usually slower for each search than a simple linear search. |
35.
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;
if (root == NULL)
return 0;
// set to 0 in case we don't recurse, below.
sibSum = childrenSum = 0;
if (root->sib == NULL)
sibSum = sumAll(root->sib);
if (root->firstChild == NULL)
childrenSum = sumAll(root->firstChild);
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
OR
False
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