



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




V
IR
G
IN
IA
PO
LYTECHNIC IN ST IT U TE
A N D ST A (^) TE (^) UNIVE
RS
IT
Y U (^) TPROSIM
Instructions:
Print your name in the space provided below. Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly indicate that you have done so, and label the continuation with the question number. If you want partial credit, justify your answers briefly and concisely, even when justification is not explicitly required. Thinking is encouraged. There are 20 questions, priced as marked. The maximum score is 100. When you have completed the test, sign the pledge at the bottom of this page and turn in the test. This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this examination with any student who has not taken it. You must return this test form when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code violation.
printed
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
signed
information hiding
separation of interface from implementation
encapsulation
reusability
obfuscation
None of these.
For questions 5 through 8, consider the following class, which represents a rational number (ratio of two integers like 1/2):
class Rational { friend ostream& operator<<(ostream& Out, const Rational& toPrint); private: int Top, Bottom; public: Rational(); Rational(int T, int B); bool operator==(const Rational& RHS) const; bool operator<(const Rational& RHS) const; bool operator>(const Rational& RHS) const; Rational operator+(const Rational& RHS) const; Rational operator-(const Rational& RHS) const; Rational operator*(const Rational& RHS) const; Rational operator/(const Rational& RHS) const; };
For questions 9 through 11, consider the queue template and main function:
// Queue.h template
template
#include "Queue.h"
bool In(Queue
int main() { Queue Q1; // Line 1 Queue
... return 0; }
bool In(Queue
while ( !Q.isEmpty() ) {
int Next = Q.Front(); if ( Target == Next ) return true; Q.Pop(); } return false; }
Line 1 only
Line 2 only
Line 3 only
Lines 1 and 2 only
Lines 1 and 3 only
Lines 1, 2 and 3
None of them are valid
Queue is not a type name. Neither is Queue
Q3 would be passed to In() by value. Since the template Queue doesn't implement a copy constructor, a shallow copy of Q3 will be made. Thus, the object Q in the function will store a pointer to the same array that was allocated by Q3. When the function call terminates, the template destructor will fire on Q, deleting its array. Unfortunately, that is the same array Q3 created, so after the function call Q3's array will have been destroyed. To make matters worse, Q3 will be unaware of that.
The implementation of the template will not compile because it refers to an undefined type T. When the template is instantiated (i.e., a variable is correctly declared using the template) the compiler will substitute the type parameter in the declaration for T, and produce a new class. But that cannot be done until an object based on the template is declared by the user.
For questions 12 through 17, consider the classes and main function:
class B { private: int aa; protected: void setaa(int ca) {aa = ca;} public: B(int ia = 0) {aa = ia;} int getaa() const {return aa;} };
class D : public B { private: char cc; public: D(char ic = '+') {cc = ic;} char getcc() const {return cc;} };
// assume needed #includes int main() {
D D1('m'); // Line 1 D D2; // Line 2 B B1; // Line 3 D1.setaa(5); // Line 4 cout << D1.getaa(); // Line 5 B1 = D1; // Line 6
return 0; }
The default constructor builds the base layer, and its default parameter value is 0, so the value of aa will be 0.
Here, the default constructor is used for the derived layer (and also the base layer, but that's irrelevant), so the value of cc will be '+'.
No. The call is to the inherited member function setaa(). However, B::setaa() is a protected member of the base class, so it cannot be called from client code of the derived class (or of B for that matter). B::setaa() could be called by any member function of the derived class, but that's not relevant to the question.
Yes. The call is to the inherited member function getaa(). B::getaa() is a public member of the base class, so it is also public in the derived class and can be called from client code of the derived class (or of B for that matter).
explain why not.
Yes. It is legal to assign a derived type object to a base type object. However, only the base members are copied to the target object. So in this case B1 will contain only the members of D1 that are inherited from the base class B; i.e., B will contain the integer value from D1 (which would be 0).
In that case, Con::satOK() will call Sat::chkFuel(), which will throw an exception of type string. Since the call to chkFuel() in satOK() is within a try block, and the associated catch block in satOK() is watching for a thrown string, the exception will be caught by satOK(), which will then return the value false. That will terminate the loop in main().
The constructor Con::Con() would throw an exception. There are actually two possibilities. The operator new could throw a bad_alloc exception (if it were implemented to the Standard), or the ensuing code could throw an int exception. Either way, the declaration in main() that invoked the constructor does not lie inside a try block, so the exception would not be caught. That would result in the program being terminated (abnormally).
A kennel must provide space and care for a variety of animals. Most dogs are housed in "runs" which are connected to a fenced space outside the kennel building. Small dogs, and cats, are housed in small to medium cages. Other types of animals are cooked and eaten. (Sorry, couldn't resist.) Large dogs, such as golden retrievers and Newfoundland retrievers, must be kept in very large runs and exercised twice a day. Medium dogs, such as beagles and spaniels, must be housed in average-sized runs and exercised once a day. Small dogs, such as Shih Tzu and Chihuahuas, are housed in medium cages and exercised once a day.
From this description, identify a set of potential classes, representing residents of the kennel not cages or runs, that should be related by inheritance, and draw the inheritance hierarchy for those classes below. Each base class should be shown above any classes derived from it. It is not necessary, or desirable, to show class details. You should include all relevant classes that are directly implied by the description above.
The nouns and noun phrases that indicate categories of kennel residents are underlined above. Note that "golden retriever" is a class, not an instance. The base class Animal is useful to encapsulate the characteristics that would be common to all the residents, even though no instance of it would be used. Similarly, a category such as "large dog" is useful because it encapsulates the characteristics common to all large dogs. We have then the following hierarchy:
Animal
Cat Dog
Small dog Medium dog Large dog
Shih Tzu Chihuahua beagle spaniel Golden retr. Newf. retr.
Note that, for instance, Shih Tzu is a class, not an instance. An individual Shih Tzu is an instance.